用正态曲线绘制直方图并命名 seaborn 中的 bin [英] Plot a histogram with normal curve and name the bins in seaborn

查看:56
本文介绍了用正态曲线绘制直方图并命名 seaborn 中的 bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试使用带有其他数据集的seaborn绘制以下类型的绘图.问题是当使用直方图类型时,即使它提供了内核曲线,我也无法命名垃圾箱(如2-2.5、2.5-3..etc).条形图不具有绘制正常曲线的功能,如图所示.该图像似乎是我不了解的SPSS统计软件包.

Hi all, I am trying to plot the following type of plot using seaborn with a different data set. The problem is when a histogram type is used, I cannot name the bins (like 2-2.5,2.5-3..etc) even though it provides kernel curves. Bar plots dont have function to draw the normal curve like in the picture. The image seems to be used SPSS statistical package which I have little knowledge of.

以下是我能得到的最接近的东西(我附上了代码)

Following is the closest thing I can get (I have attached the code)

df = pd.DataFrame({'cat': ['1-1.5', '1.5-2', '2-2.5','2.5-3','3-3.5','3.5-4','4-4.5','4.5-5'],'val': [0,0,1,7,7,33,17,10]})
ax = sns.barplot(y = 'val', x = 'cat', 
              data = df)
ax.set(xlabel='Categories', ylabel='Frequency')
plt.show()

推荐答案

所以问题当然是你没有原始数据,而是已经分箱的数据.可以反转这种分箱并从一组原始数据开始.然后再次执行直方图并使用 sns.distplot,默认情况下,它也显示 KDE 图.

So the problem is of course that you don't have the original data, but data that has already been binned. One could reverse this binning and start with an array of raw data. Then perform the histogramming again and use a sns.distplot which, by default, shows a KDE plot as well.

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

cat = ['1-1.5', '1.5-2', '2-2.5','2.5-3','3-3.5','3.5-4','4-4.5','4.5-5']
val = [0,0,1,7,7,33,17,10]
data = []
for i in range(len(cat)):
    data.extend([1.25+i*0.5]*val[i])
bins = np.arange(1,5.5, 0.5)

ax = sns.distplot(data, bins=bins, hist_kws= dict(edgecolor="k"))
ax.set(xlabel='Categories', ylabel='Frequency')
ax.set_xticks(bins[:-1]+0.25)
ax.set_xticklabels(cat)

plt.show()

使用 KDE 函数的 bw 关键字参数来设置曲线的平滑度.例如.sns.distplot(data, bins=bins, kde_kws=dict(bw=0.5), hist_kws= dict(edgecolor="k")) 其中 bw=0.5 产生

Use the bw keyword argument to the KDE function to set the smoothness of the curve. E.g. sns.distplot(data, bins=bins, kde_kws=dict(bw=0.5), hist_kws= dict(edgecolor="k")) where bw=0.5 produces

还尝试 bw = 0.1 bw = 0.25 bw = 0.35 bw = 2 差异.

这篇关于用正态曲线绘制直方图并命名 seaborn 中的 bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆