1

The following code should produce a histrogram count plot for the 2 arrays.


import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

sns.set(style="whitegrid")

data_a = [1, 2, 3]
data_b = [2.4, 2.5, 2.6]

sns.histplot(np.array(data_a),    color="red", binwidth=0.01, stat="count")
sns.histplot(np.array(data_b),    color="blue", binwidth=0.01, stat="count")

plt.savefig("output.png")

Resulting plot

Why does it only show two blue bars for the three points in the array. The data point 2.6 is ignored. The longer I think about this the more I suspect that this is a bug in the library. Or do I miss something?

I am using: python: 3.9.10, seaborn:0.11.2, seaborn-base: 0.11.2

1 Answer 1

1
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="whitegrid")

data_a = [1, 2, 3]
data_b = [2.4, 2.5, 2.6]

sns.histplot([data_a, data_b], palette=['red', 'blue'], binwidth=0.01, stat="count")
plt.savefig("output.png")
plt.show()

I can suggest the following option. Fill in the data for once, then it will work. And don't forget to vote for the answer if it helped you (check the box that is below the upper and lower triangle and click on the upper triangle). enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Many thanks. This is interesting! Is there a reason, it should work if it is entered in one line.
If I only use the command "sns.histplot(np.array(data_b), color="blue", binwidth=0.01, stat="count")" and comment out the other sns.histplot command, the peak is also not drawn.
If you do, 'binwidth=0.015' then it works. To be honest, I myself cannot understand why this is so. Information on a similar topic cannot be found. You can also try the 'sns' style and the rest is matplotlib. Now can vote.
I now decided to open an issue in the seaborn Github repository, since I am more and more convinced that it is not a wrong usage but a bug in the library. Many thanks again.
According to the github/seaborn page, the bug has been fixed and is supposed to be in v0.12.0. Many thanks to mwaskom. Most probably, it will take some time until it appears in a stable branch that is distibuted with package managers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.