mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.18k stars 1.89k forks source link

Seaborn objects interface: Log transformed bar plot does not plot correctly #3703

Closed Kartoffelecke closed 1 month ago

Kartoffelecke commented 1 month ago

I have a quick question regarding the seaborn objects interface. I am trying to simply generate a dodged bar plot with a log scaled y-axis. The bar plot renders without any problem, but as soon as I pass .scale(y="log") to the Plot object I get the error Ill-defined clip_path detected and instead of a bar, only a line is plotted. Plotting via the classic interface works fine, which is why I think this might be a bug.

Here is the example code and output:

import seaborn.objects as so
from seaborn import axes_style
import seaborn as sns
import random

# Generate some example dataframe
dfl_filtered = pd.DataFrame({'Analyte Name':['group 1','group 1', 'group 2','group 2'],
                             'Protein':['protein 1', 'protein 2', 'protein 1', 'protein 2'],
                             'c':[4000, 90000, 6000, 200000]})
# Plot with linear scale
p = (
    so.Plot(dfl_filtered, x='Analyte Name', y='c', )
    .add(so.Bar(edgecolor="black"), so.Agg(), so.Dodge(), color='Protein')
    .add(so.Range(color="black"), so.Est(errorbar="se"), so.Dodge(), color='Protein')
    .label(x="", y="c [ng/mL]")
    .theme(axes_style("ticks"))
    .layout(size=(4,3))
)
p.show()

# Plot with log scale
p = (
    so.Plot(dfl_filtered, x='Analyte Name', y='c', )
    .add(so.Bar(edgecolor="black"), so.Agg(), so.Dodge(), color='Protein')
    .add(so.Range(color="black"), so.Est(errorbar="se"), so.Dodge(), color='Protein')
    .label(x="", y="c [ng/mL]")
    .theme(axes_style("ticks"))
    .layout(size=(4,3))
    .scale(y="log")
)
p.show()

# Plot without the objects interface
fig, ax = plt.subplots()

sns.barplot(dfl_filtered, x='Analyte Name', y='c', hue='Protein', dodge=True)
ax.set_yscale("log")
fig.show()

linear bar plot with objects interface: objects_linear log-scaled bar plot with objects interface: objects_log log scaled bar plot with classical interface classic_log

mwaskom commented 1 month ago

Duplicate of https://github.com/mwaskom/seaborn/issues/3032