[英]How to make line plot following histogram bins in python
我正在嘗試使用垃圾箱數量創建歷史記錄 plot。 之后我想在 bins 后面畫一條線 plot 但我無法畫線 plot。我能得到一些幫助嗎?
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()
這是我的代碼。 Plot 我得到的是這樣的:
如果你注意到,線 plot 是一種僅在 0 中的直線。
如何在所有垃圾箱之后畫線?
如果您只想跟蹤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
設置為 plot 概率密度而不是原始計數clip
到直方圖范圍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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.