如何根据直方图箱制作线图

问题描述 投票:0回答:1

我正在尝试制作包含垃圾箱数量的历史图。 之后,我想在垃圾箱后面绘制线图,但我无法绘制线图。我可以寻求帮助吗?

plt.hist(df1_small['fz'], bins=[-5, -4.5, -4, -3.5, -3,-2.5,-2,-1.5,-1,-0.5,0, 0.5, 1,1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5])
sns.kdeplot(df1_small['fz'],fill=True, color = 'Red') 
df1_small['fz'].plot(kind = "kde")
plt.xlabel('Distribution of fz of small particles')
plt.xlim(-5, 5)
plt.show()

这是我的代码。 我得到的情节是这样的:

如果您注意到了,线图只是 0 的直线。

如何在所有垃圾箱后面画线?

数据在这里:https://github.com/Laudarisd/csv

python python-3.x matplotlib seaborn histogram
1个回答
1
投票

如果您只想追踪

plt.hist
的轮廓,请使用返回的
counts
bins
:

width = 0.5
counts, bins, bars = plt.hist(data=df1_small, x='fz', bins=np.arange(-5, 5.5, width))
plt.plot(bins[:-1] + width/2, counts)


如果您尝试覆盖

sns.kdeplot

  • 在直方图上设置
    density=True
    以绘制概率密度而不是原始计数
  • clip
    KDE 到直方图范围
  • 降低平滑带宽系数
    bw_adjust
plt.hist(data=df1_small, x='fz', bins=np.arange(-5, 5.5, 0.5), density=True)
sns.kdeplot(data=df1_small, x='fz', clip=(-5, 5), bw_adjust=0.1)

© www.soinside.com 2019 - 2024. All rights reserved.