Python Seaborn distplot()不会在y轴上显示频率

Python Seaborn distplot()不会在y轴上显示频率,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我试图在seaborn.distplot图形的y轴上显示加权频率,但它会一直显示密度,这是distplot中的默认值 我读了这本书,还有很多类似的问题 常见的答案是设置norm_hist=False,并在标准直方图中指定凹凸数组中的权重。但是,它始终显示密度,而不是每个箱子的概率/频率 我的代码是 plt.figure(figsize=(10, 4)) plt.xlim(-0.145,0.145) plt.axvline(0, color='grey') data = df['col1'] x

我试图在seaborn.distplot图形的y轴上显示加权频率,但它会一直显示密度,这是distplot中的默认值

我读了这本书,还有很多类似的问题

常见的答案是设置norm_hist=False,并在标准直方图中指定凹凸数组中的权重。但是,它始终显示密度,而不是每个箱子的概率/频率

我的代码是

plt.figure(figsize=(10, 4))
plt.xlim(-0.145,0.145)
plt.axvline(0, color='grey')
data = df['col1']

x = np.random.normal(data.mean(), scale=data.std(), size=(100000))
normal_dist =sns.distplot(x, hist=False,color="red",label="Gaussian")

data_viz = sns.distplot(data,color="blue", bins=31,label="data", norm_hist=False)

# I also tried adding the weights inside the argument
#hist_kws={'weights': np.ones(len(data))/len(data)})

plt.legend(bbox_to_anchor=(1, 1), loc=1)

我一直收到这个输出:

有人知道这里可能有什么问题吗

谢谢

[编辑]:问题是y轴显示的是KDE值,而不是加权直方图中的值。如果我设置kde=False,那么我可以在y轴上显示频率。但是,我仍然希望保留kde,因此我不考虑该选项。

将kde和频率/计数保留在一个绘图中的一个y轴上将不起作用,因为它们具有不同的比例。因此,最好创建一个具有两个轴的图,每个轴分别显示kde和直方图。 从文档norm_hist(如果为真),直方图高度显示的是密度而不是计数**如果绘制了KDE或拟合密度**,则表明这一点

中的Versunsnja有一个解决方法:

# Plot hist without kde.
# Create another Y axis.
# Plot kde without hist on the second Y axis.
# Remove Y ticks from the second axis.

first_ax  = sns.distplot(data, kde=False)
second_ax = ax.twinx()
sns.distplot(data, ax=second_ax, kde=True, hist=False)
second_ax.set_yticks([])

如果你需要这个只是为了可视化,它应该足够好了

谢谢!然而,这个解决方案只给出了直方图,因为KDE在其他尺度上。看到github post,似乎无法强制轴显示加权箱数:/