Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MrMho opened this issue Sep 23, 2024 · 1 comment

Comments

@MrMho
Copy link

MrMho commented Sep 23, 2024

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
Copy link
Owner

mwaskom commented Oct 15, 2024

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:

  • Use the so.Path mark instead of so.Line (this avoids sorting — which is unnecessary after the so.PolyFit transform anyway — so it's also marginally faster)
  • Pass orient="x" explicitly in the layer where you add the line fit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants