python之plt.hist函數(shù)的輸入?yún)?shù)和返回值的用法解釋
更新時間:2023年10月27日 14:35:05 作者:show-er-打怪之路
這篇文章主要介紹了python之plt.hist函數(shù)的輸入?yún)?shù)和返回值的用法解釋,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
函數(shù)作用----繪制直方圖
函數(shù)參數(shù)和返回值
n,bins,patches=matplotlib.pyplot.hist( x, bins=10, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype=u'bar', align=u'mid', orientation=u'vertical', rwidth=None, log=False, color=None, label=None, stacked=False, hold=None, **kwargs) '''
參數(shù)值:
hist的參數(shù)非常多,但常用的有以下6個,只有第一個是必須的,后面5個可選
x
:作直方圖所要用的數(shù)據(jù),必須是一維數(shù)組。多維數(shù)組可以先進行扁平化再作圖bins
:直方圖的柱數(shù),可選項,默認為10normed
:是否將得到的直方圖向量歸一化。默認為0facecolor
:直方圖顏色edgecolor
:直方圖邊框顏色alpha
:透明度histtype
:直方圖類型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
返回值:
n
:直方圖向量,是否歸一化由參數(shù)normed設定。當normed取默認值時,n即為直方圖各組內(nèi)元素的數(shù)量(各組頻數(shù))bins
:返回各個bin的區(qū)間范圍patches
:返回每個bin里面包含的數(shù)據(jù),是一個list
代碼示例
#導入模塊 import numpy as np import matplotlib.pyplot as plt import scipy.stats as stats #導入數(shù)據(jù)存放在ndarray中 data = np.loadtxt('eg1d1data.csv',delimiter=',') #(1)作直方圖 data=np.ravel(data) #將數(shù)組扁平化 #print(data) nbins=9 #分組數(shù) nt,bins,patches=plt.hist(data,nbins) #使用函數(shù)畫直方圖 #nt 返回 每個bin里元素的數(shù)量;bins 返回每個bin的區(qū)間范圍;patches返回每個bin里面包含的數(shù)據(jù),是一個list plt.rcParams['font.sans-serif']=['SimHei']#正常顯示中文漢字 plt.xlabel("蛋白含量(分組)",fontsize=14) plt.ylabel("頻數(shù)",fontsize=14) plt.title("100名女生測定血清蛋白含量--直方圖",fontsize=14) plt.show() print(nt,bins,patches)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python plt.imshow函數(shù)及其參數(shù)使用
plt.imshow()是Matplotlib庫中的一個函數(shù),主要用于顯示圖像或矩陣數(shù)據(jù),本文主要介紹了Python plt.imshow函數(shù)及其參數(shù)使用,具有一定的參考價值,感興趣的可以了解一下2024-02-02