'Is there a way to show the probability within hue category in Seaborn histogram

For example, I have a dataframe:

data = {'1': [1, 1, 2,2,2,2,2, 3,2.5,2.5,1.5,2,2.5,2, 2, 3,1.5,2],
       '2': [1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, 0,0,0],
       }
df = pd.DataFrame(data).T
df

df

I would like to draw a histogram in Seaborn,

import seaborn as sns
sns.histplot(data=df, x="1", hue='2',stat='probability')

histogram

Due to class 0 having few records, the bar is quite low (I want it's max height the same as class 1) even if I add a "stat" parameter of 'probability'.

It's because it's the "probability" out of total, is there another kind of "probability" exactly out of the hue category (0 and 1 here)?



Solution 1:[1]

As @mwaskom mentioned, this can be achieved combining stats='probability' and common_norm=False:

sns.histplot(data=df, x="1", hue='2',stat='probability', common_norm=False)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dopexxx