mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.48k stars 1.92k forks source link

so.PolyFit in conjunction with so.Plot.pair: Fit values connected in wrong order? #3758

Open MrMho opened 3 weeks ago

MrMho commented 3 weeks ago

When I use so.PolyFit (with order > 1) together with so.Plot.pair like so:

import seaborn.objects as so
import numpy as np
import pandas as pd

np.random.seed(1)
a = np.random.randn(20)
b = np.random.randn(20)
y = np.random.randn(20)
df = pd.DataFrame({"a": a, "b": b, "y": y})

(
so.Plot(df, y="y")
    .pair(x=["a", "b"])
    .add(so.Dots(pointsize=8))
    .add(so.Line(color='C1', linestyle='--'), so.PolyFit(order=3))
)

I get the following output:

image

It looks like the orange lines connect the values of the calculated fit in the wrong order.

numpy==1.24.2 pandas==2.0.0 matplotlib==3.7.1 seaborn==0.13.2

mwaskom commented 2 days ago

Looks like the issue here is the pairing confusing the automated detection of plot orientation, causing the Line mark to sort its data incorrectly. I see two workarounds that produce the expected result with your example: