欧美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ù)類(lèi)型,縱軸表示分布情況,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib.pyplot.hist()繪制直方圖的相關(guān)資料,需要的朋友可以參考下

一、matplotlib.pyplot.hist()語(yǔ)法

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)值,可以不寫(xiě))

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)垂直,可以不寫(xiě)):

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)值可以不寫(xiě)):

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()

 三、在直方圖上畫(huà)折線圖

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)文章

  • Python將文字轉(zhuǎn)成語(yǔ)音并讀出來(lái)的實(shí)例詳解

    Python將文字轉(zhuǎn)成語(yǔ)音并讀出來(lái)的實(shí)例詳解

    今天小編就為大家分享一篇Python將文字轉(zhuǎn)成語(yǔ)音并讀出來(lái)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python之PyMongo使用總結(jié)

    python之PyMongo使用總結(jié)

    本篇文章主要介紹了python之PyMongo使用總結(jié),詳細(xì)的介紹了PyMongo模塊的使用,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-05-05
  • Python操作Jira庫(kù)常用方法解析

    Python操作Jira庫(kù)常用方法解析

    這篇文章主要介紹了Python操作Jira庫(kù)常用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python設(shè)計(jì)微型小說(shuō)網(wǎng)站(基于Django+Bootstrap框架)

    python設(shè)計(jì)微型小說(shuō)網(wǎng)站(基于Django+Bootstrap框架)

    這篇文章主要介紹了python設(shè)計(jì)微型小說(shuō)網(wǎng)站(基于Django+Bootstrap框架),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 基于Python繪制一個(gè)摸魚(yú)倒計(jì)時(shí)界面

    基于Python繪制一個(gè)摸魚(yú)倒計(jì)時(shí)界面

    前端時(shí)間推出了一個(gè)摸魚(yú)APP,這篇文章將為大家介紹基于Python繪制一個(gè)摸魚(yú)倒計(jì)時(shí)界面,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下
    2021-12-12
  • Python為什么要保留顯式的self

    Python為什么要保留顯式的self

    本文主要介紹了Python為什么要保留顯式的self,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • matplotlib圖形整合之多個(gè)子圖繪制的實(shí)例代碼

    matplotlib圖形整合之多個(gè)子圖繪制的實(shí)例代碼

    matplotlib繪制多個(gè)子圖的時(shí)候,我們可以根據(jù)自己的想法去排列子圖的順序,也可以生成不同的子圖數(shù)量,本文就詳細(xì)的介紹了matplotlib 多子圖繪制,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-04-04
  • Django中信號(hào)signals的簡(jiǎn)單使用方法

    Django中信號(hào)signals的簡(jiǎn)單使用方法

    這篇文章主要給大家介紹了關(guān)于Django中信號(hào)signals的簡(jiǎn)單使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python利用pandas和matplotlib實(shí)現(xiàn)繪制堆疊柱狀圖

    Python利用pandas和matplotlib實(shí)現(xiàn)繪制堆疊柱狀圖

    在數(shù)據(jù)可視化中,堆疊柱狀圖是一種常用的圖表類(lèi)型,它能夠清晰地展示多個(gè)類(lèi)別的數(shù)據(jù),本文將演示如何使用 Python 的 pandas 和 matplotlib 庫(kù)繪制優(yōu)化的堆疊柱狀圖,需要的可以參考下
    2023-11-11
  • opencv 圖像禮帽和圖像黑帽的實(shí)現(xiàn)

    opencv 圖像禮帽和圖像黑帽的實(shí)現(xiàn)

    這篇文章主要介紹了opencv 圖像禮帽和圖像黑帽的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論