在 Seaborn 直方图中绘制多个分布

Plot multiple distributions in Seaborn histogram

我有一个包含四列的数据框 df_sz,我想在 Seaborn 中以“并排”方式绘制每列的直方图,即直方图之间没有重叠。但是,当我 运行 以下脚本与直方图重叠时:

sns.histplot(data=df_sz, bins=50, alpha=0.5, shrink=0.8, log_scale=True, multiple='layer')

我已经尝试了 multiple 参数的所有选项,但其中 none 有效。

有什么解决办法吗?我真的需要为这个情节使用 Seaborn。 我附上了数据框的屏幕截图和生成的直方图 .

multiple= 参数似乎与 hue= 参数一起使用。为此,应将数据帧转换为 long form:

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

df_sz = pd.DataFrame({'Ann1': (2 ** np.random.uniform(1, 20, 200)).astype(int),
                      'Ann2': (2 ** np.random.uniform(1, 20, 200)).astype(int),
                      'Ann3': (2 ** np.random.uniform(1, 20, 200)).astype(int),
                      'Ann4': (2 ** np.random.uniform(1, 20, 200)).astype(int)})

fig, ax = plt.subplots(figsize=(20, 4)) # very wide figure to accomodate 200 bars
sns.histplot(data=df_sz.melt(), x='value', hue='variable', bins=50,
             alpha=0.5, shrink=0.8, log_scale=True, multiple='dodge', ax=ax)
ax.legend_.set_title('') # remove the legend title (the name of the hue column)
ax.margins(x=0.01) # less spacing
plt.tight_layout()
plt.show()