seaborn.histplot and seaborn.displot do not match.
sns.displot is kind='hist'python3.8.11, seaborn 0.11.2, and matplotlib 3.4.2bins, the density of the corresponding plots should match.
bins is passed to numpy.histogram_bin_edgesimport seaborn as sns
import matplotlib.pyplot as plt
# sample data: wide
dfw = sns.load_dataset("penguins", cache=False)[['bill_length_mm', 'bill_depth_mm']].dropna()
# sample data: long
dfl = dfw.melt(var_name='bill_size', value_name='vals')
seaborn.displot'sharex': False, though 'sharey' worksbinsfg = sns.displot(data=dfl, x='vals', col='bill_size', kde=True, stat='density', bins=12, height=4, facet_kws={'sharey': False, 'sharex': False})
plt.show()

xlim doesn't make a differencefg = sns.displot(data=dfl, x='vals', col='bill_size', kde=True, stat='density', bins=12, height=4, facet_kws={'sharey': False, 'sharex': False})
axes = fg.axes.ravel()
axes[0].set_xlim(25, 65)
axes[1].set_xlim(13, 26)
plt.show()

seaborn.histplotfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
sns.histplot(data=dfw.bill_length_mm, kde=True, stat='density', bins=12, ax=ax1)
sns.histplot(data=dfw.bill_depth_mm, kde=True, stat='density', bins=12, ax=ax2)
fig.tight_layout()
plt.show()

common_bins=False gets the histograms into the same shape, resolving the issues of ignoring bins and sharex. However, the density seems to be affected by the number of plots in the displot.
displot, then the density is 1/3 that shown in the histplot; for 2 plots, the density is 1/2.

common_bins=False gets the histograms into the same shape, resolving the issues of ignoring bins and sharex, and the density in a faceted plot is scaled by the number of data points in each facet, not the number of facets.density being split by the number of plots in displot is resolved by using common_norm=False

# displot
fg = sns.displot(data=dfl, x='vals', col='bill_size', kde=True, stat='density', bins=12, height=4,
facet_kws={'sharey': False, 'sharex': False}, common_bins=False, common_norm=False)
fg.fig.subplots_adjust(top=0.85)
fg.fig.suptitle('Displot with common_bins & common_norm as False')
plt.show()
# histplot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
sns.histplot(data=dfw.bill_length_mm, kde=True, stat='density', bins=12, ax=ax1)
sns.histplot(data=dfw.bill_depth_mm, kde=True, stat='density', bins=12, ax=ax2)
fig.subplots_adjust(top=0.85)
fig.suptitle('Histplot')
fig.tight_layout()
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With