Python爬蟲、資料分析、網站開發等案例教程視訊免費線上觀看
https://space.bilibili.com/523606542
複制
Python學習交流群:1039649593
直方圖(Histogram),又稱品質分布圖,是一種統計報告圖,由一系列高度不等的條紋表示資料分布的情況。一般用橫軸表示資料類型,縱軸表示分布情況。直方圖是數值資料分布的精确圖形表示。為了建構直方圖,第一步是将值的範圍分段,即将整個值的範圍分成一系列間隔,然後計算每個間隔中有多少值。這些值通常被指定為連續的,不重疊的變量間隔。間隔必須相鄰,并且通常是(但不是必須的)相等的大小。
繪制直方圖:
直方圖的繪制方法,使用的是
plt.hist
方法來實作,這個方法的參數以及傳回值如下:
參數:
-
:數組或者可以循環的序列。直方圖将會從這組資料中進行分組。x
-
:數字或者序列(數組/清單等)。如果是數字,代表的是要分成多少組。如果是序列,那麼就會按照序列中指定的值進行分組。比如bins
,那麼分組的時候會按照三個區間分成3組,分别是[1,2,3,4]
。[1,2)/[2,3)/[3,4]
-
:元組或者None,如果為元組,那麼指定range
劃分區間的最大值和最小值。如果x
是一個序列,那麼bins
沒有有沒有設定沒有任何影響。range
-
:預設是density
,如果等于False
,那麼将會使用頻率分布直方圖。每個條形表示的不是個數,而是True
(落在各組樣本資料的個數稱為頻數,頻數除以樣本總個數為頻率)。頻率/組距
-
:如果這個和cumulative
都等于density
,那麼傳回值的第一個參數會不斷的累加,最終等于True
。1
- 其他參數:請參考:
。https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html
傳回值:
-
:數組。每個區間内值出現的個數,如果n
,那麼這個将傳回的是density=True
。頻率/組距
-
:數組。區間的值。bins
-
:數組。每根條的對象,類型是patches
。matplotlib.patches.Rectangle
案例:
比如有一組電影票房時長,想要看下這組票房時長的資料,那麼可以通過以下代碼來實作:
durations = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92,121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101,131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]
plt.figure(figsize=(15,5))
nums,bins,patches = plt.hist(durations,bins=20,edgecolor='k')
plt.xticks(bins,bins)
for num,bin in zip(nums,bins):
plt.annotate(num,xy=(bin,num),xytext=(bin+1.5,num+0.5))
plt.show()
複制
效果圖如下:
另外,也可以通過density=True,來實作頻率分布直方圖。示例代碼如下:
nums,bins,patches = plt.hist(durations,bins=20,edgecolor='k',density=True)
plt.xticks(bins,bins)
for num,bin in zip(nums,bins):
plt.annotate("%.4f"%num,xy=(bin,num),xytext=(bin+0.2,num+0.0005))
複制
而如果想要讓nums的總和為1,那麼就需要設定cumulative=True參數,示例代碼如下:
nums,bins,patches = plt.hist(durations,bins=20,edgecolor='k',density=True,cumulative=True)
plt.xticks(bins,bins)
for num,bin in zip(nums,bins):
plt.annotate("%.4f"%num,xy=(bin,num),xytext=(bin+0.2,num+0.0005))
複制
直方圖的應用場景:
- 顯示各組資料數量分布的情況。
- 用于觀察異常或孤立資料。
- 抽取的樣本數量過小,将會産生較大誤差,可信度低,也就失去了統計的意義。是以,樣本數不應少于50個。