1、直方圖
matplotlib.pyplot.hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, hold=None, data=None, **kwargs)
代碼示例:
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
# example data
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)
num_bins = 50
# the histogram of the data
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')
# Tweak spacing to prevent clipping of ylabel
plt.subplots_adjust(left=0.15)
plt.show()
結果如下:
函數參數解釋:
x,輸入數據,可以是一個一維序列,或多個不同長度的一維序列,或一個二維數組(等同於多個等長的一維序列)
bins,分段區間,可以是一個整數或一個序列,如果是整數n,將會均勻分爲n個區間,否則按指定的序列進行分段
range,指定橫座標的起始和終止範圍,默認範圍是(x.min(), x.max()),也可由用戶自行指定,如range=(min, max)。另外,如果bins是按指定序列分段,那麼range參數設置是無效的
normed,是否直方圖歸一化,默認爲False,如果設爲True,將會調整縱座標的值,使得直方圖中每個長方形塊的面積之和爲1,注意,不是每個長方形塊的縱座標值之和爲1
weights,默認爲False,統計直方圖時將對x中每個樣本進行加1計數,如果設爲一個與x等長的序列,那麼就不再是加1計數,而是權值求和
cumulative,是否累加,默認爲False,如果爲True,後一個直方圖將累加前面的直方圖計數
bottom,默認爲None,所有長方形塊的縱座標起始值是0,可以設爲一個整數或一個序列
orientation ,可設爲‘horizontal’或‘vertical’
log,默認爲False,如果爲True,橫座標將變成對數座標,即log(x)
color,默認爲藍色,設置顏色直接輸入顏色的英文單詞即可,如color='red'
以上參數是常用的函數參數,更多的參數可以查閱手冊http://matplotlib.org/api/pyplot_api.html
函數返回值:
n,每個長方形塊的縱座標值
bins,每個長方形塊的左右橫座標值
patches,Patch objects列表,長度等於n,裏面不知道是什麼數據