最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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è)置長條形的數(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è)置條形邊界值的對其方式,默認(rèn)為mid,除此還有'left'和'right'
    orientation={'vertical', 'horizontal'},# 設(shè)置直方圖的擺放方向,默認(rèn)為垂直方向vertical
    rwidth,# 設(shè)置直方圖條形寬度的百分比
    log=True or False,# 是否需要對繪圖數(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è)置長條形顏色(和color效果一致,設(shè)置color就不用再設(shè)置facecolor)
    edgecolor,# 設(shè)置邊框的顏色
    alpha # 設(shè)置透明度  
)
# 注意組距,得到滿意的展示效果
# 注意y軸所代表的變量是頻數(shù)還是頻率

二、繪制直方圖

①繪制簡單直方圖

import matplotlib.pyplot as plt
import numpy as np
 
data=np.random.randint(140,180,200)
 
# bins設(shè)置長條形的數(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è)置條形邊界值的對其方式,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值,對繪圖數(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è)置長條形的個(gè)數(shù)
# histtype設(shè)置樣式 barstacked:在柱狀形數(shù)據(jù)重疊并排(相同的在一起)
# range顯示范圍
# cumulative累計(jì)頻數(shù)
# align設(shè)置邊界對齊值為中心對齊
# orientation設(shè)置擺放方向?yàn)閔orizontal水平方向
# rwidth設(shè)置長條形寬度的百分比為20
# color設(shè)置長條形的填充顏色為#FFB6C1
# label設(shè)置直方圖的標(biāo)簽
# edgecolor設(shè)置長條形邊框線為#FFD700
# alpha設(shè)置長條形的透明度為0.5
# density=True 長條形呈水平方向:density將x軸轉(zhuǎn)換為密度刻度  長條形呈垂直方向:density將y軸轉(zhuǎn)換為密度刻度
# weights=data為每個(gè)數(shù)據(jù)點(diǎn)設(shè)置權(quán)重
# bottom設(shè)置基準(zhǔn)線為15000
# log=True是否對數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

    python之PyMongo使用總結(jié)

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

    Python操作Jira庫常用方法解析

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

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

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

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

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

    Python為什么要保留顯式的self

    本文主要介紹了Python為什么要保留顯式的self,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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中信號signals的簡單使用方法

    Django中信號signals的簡單使用方法

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

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

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

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

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

最新評論

阳江市| 南乐县| 那坡县| 崇阳县| 合阳县| 松溪县| 上思县| 泽库县| 抚宁县| 奈曼旗| 隆回县| 辉县市| 平江县| 平阴县| 汝城县| 清水河县| 东山县| 许昌市| 江源县| 万荣县| 噶尔县| 凤山县| 依安县| 南郑县| 浦县| 邯郸市| 邵阳市| 霸州市| 盐津县| 呼玛县| 桂东县| 金秀| 嘉兴市| 武鸣县| 萨迦县| 罗定市| 白河县| 葵青区| 公主岭市| 米易县| 香格里拉县|