简体   繁体   中英

Plot overlapping histogram plot in seaborn

I want to plot two histplots that overlap nicely in seaborn, this is my code:

sns.histplot(data=data[feature_1], color='r')
sns.histplot(data=data[data.feature_2 == 1][feature_1], color='g')

It gives the result I want for 2 possible values: 在此处输入图像描述

But for multiple values it doesn't fit nicely: 在此处输入图像描述

Any thoughts or advise to make this look nice?

The problem in your second plot is that the histogram is defining some number of bins (probably about 10; it depends on the size and variance of the dataset) between the minimum and maximum value in the data. That is not ideal when you have a small number of integer values. If you add discrete=True it will define bins centered on every number between (inclusive) the minimum and maximum value, producing a similar effect to what you get for a histogram over categorical data.

With categorical data it is easy to infer that you want bins to correspond to unique data values. It is more difficult when data are numeric. You might think it makes sense if the dtype is integer, but obvious counterexamples come to mind. Imagine a histogram of ages in a population, or even worse, salaries. Unit-width bins would be too small in those cases. So discrete binning is strictly opt-in for numeric data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM