简体   繁体   English

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

[英]Seaborn distplot() won't display frequency in the y-axis

I am trying to display the weighted frequency in the y-axis of a seaborn.distplot() graph, but it keeps displaying the density (which is the default in distplot() )我试图在seaborn.distplot()图的 y 轴上显示加权频率,但它一直显示密度(这是distplot()中的默认值)

I read the documentation and also many similar questions here in Stack.我在 Stack 中阅读了文档以及许多类似的问题。

The common answer is to set norm_hist=False and also to assign the weights in a bumpy array as in a standard histogram.常见的答案是设置norm_hist=False并像在标准直方图中一样在凹凸不平的数组中分配权重。 However, it keeps showing the density and not the probability/frequency of each bin.但是,它一直显示密度而不是每个 bin 的概率/频率。

My code is我的代码是

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)

And I keep receiving this output:我不断收到这个 output:

在此处输入图像描述

Does anyone have an idea of what could be the problem here?有谁知道这里可能是什么问题?

Thanks!谢谢!

[EDIT]: The problem is that the y-axis is showing the kde values and not those from the weighted histogram. [编辑]:问题在于 y 轴显示的是kde值,而不是加权直方图中的值。 If I set kde=False then I can display the frequency in the y-axis.如果我设置kde=False那么我可以在 y 轴上显示频率。 However, I still want to keep the kde , so I am not considering that option.但是,我仍然想保留kde ,所以我没有考虑这个选项。

Keeping the kde and the frequency/count in one y-axis in one plot will not work because they have different scales.kdefrequency/count保持在一个 plot 中的一个 y 轴将不起作用,因为它们具有不同的比例。 So it might be better to create a plot with 2 axis with each showing the kde and histogram separately.因此,最好创建一个具有 2 个轴的 plot,每个轴分别显示 kde 和直方图。 From documentation norm_hist If True, the histogram height shows a density rather than a count. **This is implied if a KDE or fitted density is plotted**.从文档norm_hist If True, the histogram height shows a density rather than a count. **This is implied if a KDE or fitted density is plotted**. If True, the histogram height shows a density rather than a count. **This is implied if a KDE or fitted density is plotted**.

versusnja in https://github.com/mwaskom/seaborn/issues/479 has a workaround: versusnja ://github.com/mwaskom/seaborn/issues/479中的 vsnja 有一个解决方法:

# 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([])

If you need this just for visualization it should be good enough.如果你需要这个只是为了可视化它应该足够好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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