seaborn icon indicating copy to clipboard operation
seaborn copied to clipboard

sns.histplot does not draw correct bins when using log for y-axis.

Open SimonHedrich opened this issue 1 year ago • 3 comments

I'm stuck on a frustrating issue. There is a Jupyter Notebook that I created about a year ago. It contains some histograms. When running the notebook again I noticed that now the drawing of the bins are broken when using log for the y-axis. Unfortunately I don't know what python and library versions I was using then.

I tried different versions of python, seaborn, numpy, pandas and matplotlib; and also ran it on both on MacOS and Google Colab. The following are the latest versions that also result in the problem: python==3.10.12 seaborn==0.13.2 matplotlib==3.8.2 pandas==2.2.0 numpy==1.26.0

This is the code that produces the plots below:

import seaborn as sns
data = [41, 44, 46, 46, 47, 47, 48] # shortened
plot = sns.histplot(
    data,
    log_scale=(x_log, y_log)
)

Depending on what I insert for (x_log, y_log) I get different plots(in terms of how broken they are):

(False, True) plot_false_true (True, False) plot_true_false (True, True) plot_true_true (False, False) plot_false_false

Is there a combination of library and python version that work for this or am I doing something wrong? Thank you for all your help!

SimonHedrich avatar Jan 28 '24 21:01 SimonHedrich

I don't think it's the bins that are wrong here, it's the fact that the bars being drawn from 0 introduces nans when you plot on a log axis.

mwaskom avatar Feb 01 '24 14:02 mwaskom

It looks like in this case actually you may want to use the matplotlib api to set the y scale to log after plotting with seaborn.

mwaskom avatar Feb 01 '24 14:02 mwaskom

Thank you for your answer! The following code works well:

import matplotlib.pyplot as plt
import seaborn as sns
data = [41, 44, 46, 46, 47, 47, 48, 48, 49, 51, 52, 53, 53]

plot = sns.histplot(data)
plt.yscale('log')
plt.show()

SimonHedrich avatar Feb 01 '24 15:02 SimonHedrich