4.6Python數據處理篇之Matplotlib系列(六)---plt.hist()與plt.hist2d()直方圖


目錄

前言

今天我們學習的是直方圖,導入的函數是:

plt.hist(x=x, bins=10) 與plt.hist2D(x=x, y=y)

(一)直方圖

(1)說明:

pyplot.``hist(x, bins=None, density=None,……kwargs*)

常見的參數屬性

具體參考:官網說明文檔

屬性 說明 類型
x 數據 數值類型
bins 條形數 int
color 顏色 "r","g","y","c"
density 是否以密度的形式顯示 bool
range x軸的范圍 數值元組(起,終)
bottom y軸的起始位置 數值類型
histtype 線條的類型 "bar":方形,"barstacked":柱形,
"step":"未填充線條"
"stepfilled":"填充線條"
align 對齊方式 "left":左,"mid":中間,"right":右
orientation orientation "horizontal":水平,"vertical":垂直
log 單位是否以科學計術法 bool

(2)源代碼:

# 導入模塊
import matplotlib.pyplot as plt
import numpy as np

# 數據
mu = 100  # 均值
sigma = 20  # 方差
# 2000個數據
x = mu + sigma*np.random.randn(2000)


# 畫圖 bins:條形的個數, normed:是否標准化
plt.hist(x=x, bins=10)


# 展示
plt.show()

(3)輸出效果:

默認:y軸是個數

01.png

改:plt.hist(x=x, bins=10, density=True)

y軸是頻率

02.png

(二)雙直方圖

(1)說明:

pyplot.``hist2d(x, y, bins=10, **kwargs)

常見的參數屬性

具體參考:官網說明文檔

x x坐標
y y坐標
bins 橫豎分為幾條

(2)源代碼:

# 導入模塊
import matplotlib.pyplot as plt
import numpy as np

# 數據
x = np.random.randn(1000)+2
y = np.random.randn(1000)+3

# 畫圖
plt.hist2d(x=x, y=y, bins=30)

# 展示
plt.show()

(3)輸出效果:

03.png

作者:Mark

日期:2019/02/13 周三


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM