seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

seaborn.objects incorrectly plots secondary y-axis

Open SamKG opened this issue 1 year ago • 1 comments

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

SamKG avatar Jan 10 '24 11:01 SamKG

Yeah that's a weird one! I don't have any immediate hypotheses, unfortunately.

mwaskom avatar Jan 16 '24 12:01 mwaskom