seaborn
seaborn copied to clipboard
sns.histplot does not draw correct bins when using log for y-axis.
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)
(True, False)
(True, True)
(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!
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.
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.
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()