Skip to content

seaborn.objects incorrectly plots secondary y-axis #3614

@SamKG

Description

@SamKG

Hello,
Thanks for the great work on seaborn.objects, I really like it.

I noticed an issue however when trying to plot on a secondary y-axis using Plot.on(). Essentially, the second axis seems to be double-plotted - once using the original axis, and again using the correct second axis. This leads to overlapping ticks and the grid lines being drawn above the first plot's line.

Minimal reproduction:

from matplotlib import pyplot as plt
import seaborn as sns
import seaborn.objects as so
import pandas as pd

# create simple dataframe

df = pd.DataFrame(
    {
        "X": [1, 2, 3, 4],
        "Y1": [1, 2, 3, 4],
        "Y2": [0, 3, 9, 81],
    }
)

sns.set_theme()

f = plt.figure(figsize=(8, 4))
# get the axis
ax = f.add_subplot(111)
ax2 = ax.twinx()

(
    so.Plot(df, x="X", y="Y1")
    .add(so.Line(color="C0"))
    .on(ax)
    .plot()
)

(
    so.Plot(df, x="X", y="Y2")
    .add(so.Line(color="C1"))
    .on(ax2)
    .plot()
)

plt.show()

image

I managed a temporary workaround using the following:

ax2.grid(False)
ax2.yaxis.tick_right()

but it's still weird that it happens to begin with. I'd be happy to take a stab at fixing it if anyone could point me in the right direction

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions