目錄
目錄
前言
今天我們學習的是直方圖,導入的函數是:
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軸是個數

改:plt.hist(x=x, bins=10, density=True)
y軸是頻率

(二)雙直方圖
(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)輸出效果:

