Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SimonHedrich opened this issue Jan 28, 2024 · 3 comments
Open

Comments

@SimonHedrich
Copy link

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 SimonHedrich changed the title sns.histplot does not draw correct bins. sns.histplot does not draw correct bins when using log for y-axis. Jan 28, 2024
@mwaskom
Copy link
Owner

mwaskom commented Feb 1, 2024

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
Copy link
Owner

mwaskom commented Feb 1, 2024

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.

@SimonHedrich
Copy link
Author

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants