Skip to content

Superfluous legend entries when plotting with seaplot #405

Closed

Description

@lschr

Using seaborn's pointplot and lineplot, extra legend entries appear with ultraplot, but not with matplotlib.

import pandas as pd
import seaborn
import ultraplot as uplt

fig, ax = uplt.subplots()
df = pd.DataFrame(
    {
        "xcol": np.concatenate([np.arange(10)] * 2),
        "ycol": np.concatenate([np.arange(10), 1.5 * np.arange(10)]),
        "hcol": ["h1"] * 10 + ["h2"] * 10,
    }
)

seaborn.lineplot(df, x="xcol", y="ycol", hue="hcol")

This results in
Image

With fig, ax = matplotlib.pyplot.subplots(), there are no extra entries (y, ymin),

Image

Activity

  1. cvanelteren commented on Nov 10, 2025

    @cvanelteren
    Collaborator

    Not sure how the internals of seaborn are, but we are getting the labels from the objects themselves. It seems like seaborn is adding them in their lineplot. I would suggest setting them manually

    for x, dfi in df.groupby("hcol"):
        ax.plot(dfi.xcol, dfi.ycol, label=x)
    ax.legend()
  2. cvanelteren commented on Nov 10, 2025

    @cvanelteren
    Collaborator

    From their source I can see that they iterate over it internally -- our internal mechanism will pick up it is a dataframe and then infer the labels from the (sub/grouped) dataframe. Not sure at this time how we could circumvent this other than the suggestion above. The tricky part is that when plot is called our internal plotting will take over and see it is a DataFrame and then act accordingly.

    EDIT: not sure on the extra ys tho, it implies that seaborn is called too often to get those extra entries. The shaded area is added automatically through a similar mechanism as above. The DataFrame part of UltraPlot I still need to study as I don't use this often / at all.

  3. cvanelteren commented on Nov 15, 2025

    @cvanelteren
    Collaborator

    There is a fix on #406. The issue is that seaborn internally tags the plots which we tend (erroneously) pick up. We provide a context manager to turn this feature off for external toolboxes.

  4. lschr commented on Nov 17, 2025

    @lschr
    Author

    Great, thanks a lot!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions