欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?matplotlib.pyplot.hist()繪制直方圖的方法實(shí)例

 更新時(shí)間:2022年06月30日 11:36:25   作者:小羊快學(xué)  
直方圖(Histogram)又稱質(zhì)量分布圖,是一種統(tǒng)計(jì)報(bào)告圖,由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況,一般用橫軸表示數(shù)據(jù)類型,縱軸表示分布情況,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib.pyplot.hist()繪制直方圖的相關(guān)資料,需要的朋友可以參考下

一、matplotlib.pyplot.hist()語法

hist(x, bins=None, range=None, density=False,weights=None, cumulative=False, 
bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None,
 log=False, color=None, label=None, stacked=False, *, data=None, **kwargs)
plt.hist(
    x,# 指定要繪制直方圖的數(shù)據(jù)
    bins,# 設(shè)置長(zhǎng)條形的數(shù)目
    range,# 指定直方圖數(shù)據(jù)的上下界,默認(rèn)包含繪圖數(shù)據(jù)的最大值和最小值(范圍)
    density=True or False, # 如果"True",將y軸轉(zhuǎn)化為密度刻度 默認(rèn)為None
    weights,# 該參數(shù)可為每一個(gè)數(shù)據(jù)點(diǎn)設(shè)置權(quán)重
    cumulative=True or False,# 是否需要計(jì)算累計(jì)頻數(shù)或頻率 默認(rèn)值False
    bottom=0, # 可以為直方圖的每個(gè)條形添加基準(zhǔn)線,默認(rèn)為0
    histtype={'bar', 'barstacked', 'step', 'stepfilled'} # 設(shè)置樣式
               # bar柱狀形數(shù)據(jù)并排,默認(rèn)值。
               # barstacked在柱狀形數(shù)據(jù)重疊并排(相同的在一起)
               # step柱狀形顏色不填充 
               # stepfilled填充的線性
    align='mid' or 'left' or 'right', # 設(shè)置條形邊界值的對(duì)其方式,默認(rèn)為mid,除此還有'left'和'right'
    orientation={'vertical', 'horizontal'},# 設(shè)置直方圖的擺放方向,默認(rèn)為垂直方向vertical
    rwidth,# 設(shè)置直方圖條形寬度的百分比
    log=True or False,# 是否需要對(duì)繪圖數(shù)據(jù)進(jìn)行l(wèi)og變換 默認(rèn)值False
    color='r',# 設(shè)置直方圖的填充色
    label, # 設(shè)置直方圖的標(biāo)簽
    stacked=True or False, # 當(dāng)有多個(gè)數(shù)據(jù)時(shí),是否需要將直方圖呈堆疊擺放,默認(rèn)False水平擺放;
    facecolor,# 設(shè)置長(zhǎng)條形顏色(和color效果一致,設(shè)置color就不用再設(shè)置facecolor)
    edgecolor,# 設(shè)置邊框的顏色
    alpha # 設(shè)置透明度  
)
# 注意組距,得到滿意的展示效果
# 注意y軸所代表的變量是頻數(shù)還是頻率

二、繪制直方圖

①繪制簡(jiǎn)單直方圖

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
# bins設(shè)置長(zhǎng)條形的數(shù)目
plt.hist(data,bins=10)
 
plt.show()

②:各個(gè)參數(shù)繪制的直方圖

(1)histtype參數(shù)(設(shè)置樣式bar、barstacked、step、stepfilled)

1. bar:柱狀形數(shù)據(jù)并排(因?yàn)閎ar是默認(rèn)值,可以不寫)

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10)
 
plt.show()

 2. barstacked:在柱狀形數(shù)據(jù)重疊并排(相同的在一起)

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,histtype='barstacked')
 
plt.show()

 3. step:柱狀形顏色不填充 

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,histtype='step')
 
plt.show()

 4. stepfilled:生成一個(gè)默認(rèn)填充的線圖

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,histtype='stepfilled')
 
plt.show()

(2)range參數(shù)(指定直方圖數(shù)據(jù)的上下界,默認(rèn)包含繪圖數(shù)據(jù)的最大值和最小值(范圍))

不想顯示數(shù)據(jù)全部范圍,只想查看數(shù)據(jù)某一個(gè)范圍內(nèi)的數(shù)據(jù)。(例:下圖數(shù)據(jù)范圍為140~180之間,只想查看150~170之間的數(shù)據(jù))

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,histtype='bar',range=(150,170))
 
plt.show()

(3)orientation參數(shù) (設(shè)置直方圖的擺放位置,vertical垂直方向 horizontal水平方向,默認(rèn)值:vertical垂直方向)

垂直方向(默認(rèn)垂直,可以不寫):

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10)
 
plt.show()

 

horizontal水平方向:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,orientation='horizontal')
 
plt.show()

(4)density參數(shù)(bool值,True:將坐標(biāo)軸轉(zhuǎn)化為密度刻度,默認(rèn)值:None)

直方圖為垂直方向時(shí),觀察y軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,density=True)
 
plt.show()

 直方圖為水平方向時(shí),觀察x軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,orientation='horizontal',density=True)
 
plt.show()

(5)weights參數(shù)(為每個(gè)數(shù)據(jù)點(diǎn)設(shè)置權(quán)重)

  直方圖為垂直方向時(shí),觀察y軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,weights=data)
 
plt.show()

  直方圖為水平方向時(shí),觀察x軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,orientation='horizontal',weights=data)
 
plt.show()

(6)cumulative參數(shù)(bool值,是否需要計(jì)算累計(jì)頻數(shù)或頻率,默認(rèn)值:False)

頻數(shù):指事件發(fā)生的次數(shù)

頻率:指次數(shù)占總次數(shù)n的比例

頻率=頻數(shù)/n

  直方圖為垂直方向時(shí):

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,cumulative=True)
 
plt.show()

直方圖為水平方向時(shí): 

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,orientation='horizontal',cumulative=True)
 
plt.show()

(7)bottom參數(shù)(為直方圖添加基準(zhǔn)線)

直方圖為垂直方向時(shí),觀察y軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,bottom=170)
 
plt.show()

 直方圖為水平方向時(shí),觀察x軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,orientation='horizontal',bottom=170)
 
plt.show()

(8)align參數(shù)(設(shè)置條形邊界值的對(duì)其方式,mid、left、right,默認(rèn)值:mid)

mid(默認(rèn)值可以不寫):

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10)
 
plt.show()

 left:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,align='left')
 
plt.show()

 right:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,align='right')
 
plt.show()

(9)rwidth參數(shù)(設(shè)置直方圖條形寬度的百分比)

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,rwidth=0.5)
 
plt.show()

(10)log參數(shù)(bool值,對(duì)繪圖數(shù)據(jù)進(jìn)行l(wèi)og變換 默認(rèn)值:False)

直方圖為垂直方向時(shí),觀察y軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,log=True)
 
plt.show()

 直方圖為水平方向時(shí),觀察x軸:

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
plt.hist(data,bins=10,orientation='horizontal',log=True)
 
plt.show()

(11)stacked參數(shù)(bool值,當(dāng)有多個(gè)數(shù)據(jù)時(shí),是否需要將直方圖呈堆疊擺放,默認(rèn)值:False水平擺放)

stacked=False時(shí):(水平擺放)

import matplotlib.pyplot as plt
import numpy as np
 
x=np.random.randint(140,180,200)
y=np.random.randint(140,180,200)
 
plt.hist([x,y], bins=10)
 
plt.show()

 stacked=True時(shí):(堆疊擺放)

import matplotlib.pyplot as plt
import numpy as np
 
x=np.random.randint(140,180,200)
y=np.random.randint(140,180,200)
 
plt.hist([x,y], bins=10,stacked=True)
 
plt.show()

(12)直方圖所有參數(shù)展示:

import matplotlib.pyplot as plt
import numpy as np
 
plt.rcParams['font.sans-serif']=['FangSong']
 
fig=plt.figure(figsize=(8,8))
data=np.random.randint(140,180,200)
 
# data數(shù)據(jù)
# bins設(shè)置長(zhǎng)條形的個(gè)數(shù)
# histtype設(shè)置樣式 barstacked:在柱狀形數(shù)據(jù)重疊并排(相同的在一起)
# range顯示范圍
# cumulative累計(jì)頻數(shù)
# align設(shè)置邊界對(duì)齊值為中心對(duì)齊
# orientation設(shè)置擺放方向?yàn)閔orizontal水平方向
# rwidth設(shè)置長(zhǎng)條形寬度的百分比為20
# color設(shè)置長(zhǎng)條形的填充顏色為#FFB6C1
# label設(shè)置直方圖的標(biāo)簽
# edgecolor設(shè)置長(zhǎng)條形邊框線為#FFD700
# alpha設(shè)置長(zhǎng)條形的透明度為0.5
# density=True 長(zhǎng)條形呈水平方向:density將x軸轉(zhuǎn)換為密度刻度  長(zhǎng)條形呈垂直方向:density將y軸轉(zhuǎn)換為密度刻度
# weights=data為每個(gè)數(shù)據(jù)點(diǎn)設(shè)置權(quán)重
# bottom設(shè)置基準(zhǔn)線為15000
# log=True是否對(duì)數(shù)據(jù)進(jìn)行l(wèi)og轉(zhuǎn)換
plt.hist(data,bins=10,histtype='barstacked',range=(140,170),cumulative=True,align='mid',orientation='horizontal',rwidth=20,color='#FFB6C1',
        label='數(shù)量',edgecolor='#FFD700',alpha=0.5,weights=data,bottom=10000,log=False)
 
plt.xticks(size=20) # x軸刻度值大小
plt.yticks(size=20) # y軸刻度值大小
 
plt.title('hist',size=30) # 設(shè)置直方圖標(biāo)簽
plt.xlabel('x軸',size=15) # 設(shè)置x軸標(biāo)簽
plt.ylabel('y軸',size=20) # 設(shè)置y軸標(biāo)簽
 
plt.rcParams.update({'font.size':20})  # 修改圖例字體大小
 
plt.legend()
plt.show()

 三、在直方圖上畫折線圖

import matplotlib.pyplot as plt
import numpy as np
 
x=np.random.normal(100,15,10000)
y=np.random.normal(80,15,10000)
 
# density=True設(shè)置為密度刻度
n1, bins1, patches1 = plt.hist(x, bins=50,  density=True, color='#00B8B8', alpha=1)
n2, bins2, patches2 = plt.hist(y, bins=50,  density=True, color='r', alpha=0.2)
 
plt.plot(bins1[:-1],n1,':',lw=3)
plt.plot(bins2[:-1],n2,'--',lw=3)
 
plt.show()

總結(jié)

到此這篇關(guān)于Python matplotlib.pyplot.hist()繪制直方圖的文章就介紹到這了,更多相關(guān)matplotlib.pyplot.hist()繪制直方圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • TensorFlow 實(shí)戰(zhàn)之實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的實(shí)例講解

    TensorFlow 實(shí)戰(zhàn)之實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的實(shí)例講解

    下面小編就為大家分享一篇TensorFlow 實(shí)戰(zhàn)之實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Python測(cè)試模塊doctest使用解析

    Python測(cè)試模塊doctest使用解析

    這篇文章主要介紹了Python測(cè)試模塊doctest使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python 元類實(shí)例解析

    Python 元類實(shí)例解析

    這篇文章主要介紹了 Python 元類實(shí)例解析,此文的主要任務(wù)就是給大家徹底講明白什么是元類,需要的朋友可以參考下
    2018-04-04
  • Numpy中扁平化函數(shù)ravel()和flatten()的區(qū)別詳解

    Numpy中扁平化函數(shù)ravel()和flatten()的區(qū)別詳解

    本文主要介紹了Numpy中扁平化函數(shù)ravel()和flatten()的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python深度學(xué)習(xí)albumentations數(shù)據(jù)增強(qiáng)庫

    Python深度學(xué)習(xí)albumentations數(shù)據(jù)增強(qiáng)庫

    下面開始albumenations的正式介紹,在這里我強(qiáng)烈建議英語基礎(chǔ)還好的讀者去官方網(wǎng)站跟著教程一步步學(xué)習(xí),而這里的內(nèi)容主要是我自己的一個(gè)總結(jié)以及方便英語能力較弱的讀者學(xué)習(xí)
    2021-09-09
  • 詳解OpenCV實(shí)現(xiàn)特征提取的方法

    詳解OpenCV實(shí)現(xiàn)特征提取的方法

    在本文中,我們將一起探索幾種從圖像中提取顏色、形狀和紋理特征的方法,這些方法基于處理圖像的經(jīng)驗(yàn),感興趣的小伙伴可以了解一下
    2022-05-05
  • Python學(xué)習(xí)_幾種存取xls/xlsx文件的方法總結(jié)

    Python學(xué)習(xí)_幾種存取xls/xlsx文件的方法總結(jié)

    今天小編就為大家分享一篇Python學(xué)習(xí)_幾種存取xls/xlsx文件的方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • windows系統(tǒng)Tensorflow2.x簡(jiǎn)單安裝記錄(圖文)

    windows系統(tǒng)Tensorflow2.x簡(jiǎn)單安裝記錄(圖文)

    這篇文章主要介紹了windows系統(tǒng)Tensorflow2.x簡(jiǎn)單安裝記錄(圖文),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • django富文本編輯器的實(shí)現(xiàn)示例

    django富文本編輯器的實(shí)現(xiàn)示例

    這篇文章主要介紹了django富文本編輯器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • python3.7 使用pymssql往sqlserver插入數(shù)據(jù)的方法

    python3.7 使用pymssql往sqlserver插入數(shù)據(jù)的方法

    這篇文章主要介紹了python3.7 使用pymssql往sqlserver插入數(shù)據(jù)的方法,代碼很簡(jiǎn)單,感興趣的朋友跟隨小編一起看看吧
    2019-07-07

最新評(píng)論