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

Display an artist label instead of x y not working #80

Open
arnaudvre opened this issue Aug 17, 2024 · 3 comments
Open

Display an artist label instead of x y not working #80

arnaudvre opened this issue Aug 17, 2024 · 3 comments

Comments

@arnaudvre
Copy link

arnaudvre commented Aug 17, 2024

I'm using Seaborn, instead of the artist label I get _child0 for example (number is specific to a line). Interestingly on version 0.5.2-2 (apt default) the label shows intermittently, moving to 0.5.3 (pip latest/default) only x and y.

    df = pd.DataFrame(pandas_dict_for_dataframe)

    dfm = df.melt('Date', var_name='Item', value_name='Price')

    g = sns.catplot(x="Date", y="Price", hue='Item', data=dfm, kind='point', height=8, aspect=16 / 8., dodge=2)

    plt.xticks(rotation=45, ha='right')

    mplcursors.cursor(hover=True).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
    plt.show()

image

@anntzer
Copy link
Owner

anntzer commented Aug 17, 2024

Please provide a minimal reproducible example that I can directly copy-paste and run to test the issue.

@arnaudvre
Copy link
Author

arnaudvre commented Aug 18, 2024

sure

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import mplcursors

data = {
  "target": [1, 2, 3, 4],
  "dave": [50, 44, 52, 41],
  "jane": [45, 41, 43, 39]
}

df = pd.DataFrame(data)

dfm = df.melt('target', var_name='participant', value_name='calories')

g = sns.lineplot(x="target", y="calories", hue='participant', data=dfm)

mplcursors.cursor(hover=True).connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))

plt.show()

@anntzer
Copy link
Owner

anntzer commented Aug 20, 2024

Thanks for the example. This appears to be because seaborn does not set the label properly on the relevant artist, but rather on a "fake" artist used only for the purposes of drawing the legend (see add_legend_data in seaborn's codebase). I guess they probably intentionally don't set the label at all on the real artist in order to prevent duplicate legend entries, but as a consequence, mplcursors cannot figure out the correct label.
As I don't use seaborn myself, I don't really have any suggestions for a fix.

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

No branches or pull requests

2 participants