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

Legend line is incorrect #3543

Closed
JulienManulife1 opened this issue Nov 2, 2023 · 3 comments
Closed

Legend line is incorrect #3543

JulienManulife1 opened this issue Nov 2, 2023 · 3 comments

Comments

@JulienManulife1
Copy link

JulienManulife1 commented Nov 2, 2023

Hello, so I am not sure where to post specifically. This is a bug as of Pandas 2.0.0 I believe, and is when Matplotlib plt legend is called on what I've tested so far being the relplot alone.

pandas = 2.1.2
matplotlib= 3.8.1
seaborn=0.13
image

import matplotlib.pyplot as plt
from matplotlib.pyplot import *
import seaborn as sns
import pandas as pd

fmri = sns.load_dataset("fmri")

fig = plt.figure(figsize=(12, 10))
sns.set()
ax=sns.relplot(data=fmri, x="timepoint", y="signal", col="region",
    hue="event", style="event", kind="line",legend=False)
plt.legend(bbox_to_anchor=(0,-0.2,1,0),title='', frameon=False, loc="lower center"
           , labels=fmri.event.unique(), ncol=fmri.event.nunique(),handletextpad=0.2,columnspacing=.8)
@mwaskom
Copy link
Owner

mwaskom commented Nov 2, 2023

This is not a bug in any library, just a consequence of matplotlib’s “magic” legend creation not doing what you expect. When you call legend without providing explicit artists, it grabs the first two artists that are attached to the axes; in this case that happens to be the first line and its confidence interval because that is the order that seaborn adds things to the plot.

I’m going to close because there is no action to be taken on the library side here and ample discussion of how to customize the legend in the docs and on eg stackoverflow.

@mwaskom mwaskom closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2023
@JulienManulife1
Copy link
Author

Thanks. I brought this up actually as I had experienced it when I updated pandas so it was sudden unexpected behavior. Reverting pandas caused the lines to be drawn correctly

@mwaskom
Copy link
Owner

mwaskom commented Nov 2, 2023

The point is that the specific ordering of artists in the matplotlib artist list is an implementation detail, so it’s risky to rely on it, as you do when calling legend() without a list of artist handles. I really wish this “feature” didn’t exist in matplotlib — it’s convenient for interactive, direct use of matplotlib, but it makes it too easy to get incorrect results (which may or may not be noticeable as they are here). I personally really urge you not to use it. But there’s not much seaborn can do about that.

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