国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python 實(shí)例|matplotlib|繪制直方圖(各參數(shù)樣例)

這篇具有很好參考價(jià)值的文章主要介紹了Python 實(shí)例|matplotlib|繪制直方圖(各參數(shù)樣例)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

matplotlib.pyplot.hist 的官方文檔:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html

matplotlib.pyplot.hist

這個(gè)方法使用 numpy.histogram 首先將 x 中的數(shù)據(jù)分桶并統(tǒng)計(jì)每個(gè)桶中的元素?cái)?shù)量,接著使用條形圖繪制這個(gè)分布。

函數(shù)參數(shù)、含義及樣例如下:

from matplotlib import pyplot as plt
import numpy as np

參數(shù)列表及樣例

x : 數(shù)據(jù)集對(duì)象(必填)
  • (n,) array or sequence of (n,) arrays

Input values, this takes either a single array or a sequence of arrays which are not required to be of the same length.

樣例 1:當(dāng) x 為多維向量時(shí),繪制多維向量的直方圖
plt.hist(np.random.randn(10000))
plt.title("Example 1")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

樣例 2:當(dāng) x 為 m 個(gè)多維向量的列表時(shí)(各個(gè)向量的長(zhǎng)度不需要全部相同),將在同一個(gè)軸上分別繪制 m 個(gè)多維向量的直方圖
plt.hist([
    np.random.randn(10000),
    2 * np.random.randn(10000),
    3 * np.random.randn(5000)
])
plt.title("Example 2")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

bins : 分桶數(shù) / 分桶數(shù)值(非必填)
  • int or sequence or str, default: rcParams["hist.bins"] (default: 10)

If bins is an integer, it defines the number of equal-width bins in the range.

If bins is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin; in this case, bins may be unequally spaced. All but the last (righthand-most) bin is half-open. In other words, if bins is:

[1, 2, 3, 4]

then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.

If bins is a string, it is one of the binning strategies supported by numpy.histogram_bin_edges: ‘a(chǎn)uto’, ‘fd’, ‘doane’, ‘scott’, ‘stone’, ‘rice’, ‘sturges’, or ‘sqrt’.

此參數(shù)用于傳給 numpy.histogrambins 形參,詳情見(jiàn) numpy.histogram_bin_edges 的文檔。

樣例 3:當(dāng) bins 傳入整數(shù)時(shí),會(huì)分為 bins 數(shù)量的等寬桶
plt.hist(np.random.randn(10000), bins=20)
plt.title("Example 3")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

樣例 4:當(dāng) bins 傳入序列時(shí),會(huì)視作每個(gè)桶的邊界
plt.hist(np.random.randn(10000), bins=[-3, -0.8, 0, 0.3, 3])
plt.title("Example 4")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

樣例 5:當(dāng)傳入字符串時(shí),例如 “fd
plt.hist(np.random.randn(10000), bins="fd")
plt.title("Example 5")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

range : 桶的下界和上界(非必填)
  • tuple or None, default: None

The lower and upper range of the bins. Lower and upper outliers are ignored. If not provided, range is (x.min(), x.max()). Range has no effect if bins is a sequence.

If bins is a sequence or range is specified, autoscaling is based on the specified bin range instead of the range of x.

此參數(shù)用于傳給 numpy.histogramrange 形參,詳情見(jiàn) numpy.histogram_bin_edges 的文檔。

樣例 6:使用 range 參數(shù)
plt.hist(np.random.randn(10000), range=(0, 1))
plt.title("Example 6")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

density : y 軸是否使用頻率(非必填)
  • bool, default: False

If True, draw and return a probability density: each bin will display the bin’s raw count divided by the total number of counts and the bin width (density = counts / (sum(counts) * np.diff(bins))), so that the area under the histogram integrates to 1 (np.sum(density * np.diff(bins)) == 1).

If stacked is also True, the sum of the histograms is normalized to 1.

樣例 7:顯示頻率
plt.hist(np.random.randn(10000), density=True)
plt.title("Example 7")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

weight :是否為元素賦予權(quán)重
  • (n,) array-like or None, default: None

An array of weights, of the same shape as x. Each value in x only contributes its associated weight towards the bin count (instead of 1). If density is True, the weights are normalized, so that the integral of the density over the range remains 1.

樣例 8:配置權(quán)重
x = np.random.randn(10000)
weights = 10 * (x >= 0) + 1 * (x < 0)
plt.hist(x, weights=weights)
plt.title("Example 8")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

cumulative : 是否繪制累加直方圖
  • bool or -1, default: False

If True, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values. The last bin gives the total number of datapoints.

If density is also True then the histogram is normalized such that the last bin equals 1.

If cumulative is a number less than 0 (e.g., -1), the direction of accumulation is reversed. In this case, if density is also True, then the histogram is normalized such that the first bin equals 1.

樣例 9:累加直方圖
plt.hist(np.random.randn(10000), cumulative=True)
plt.title("Example 9")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

bottom : 每個(gè)桶的初始值
  • array-like, scalar, or None, default: None

Location of the bottom of each bin, i.e. bins are drawn from bottom to bottom + hist(x, bins) If a scalar, the bottom of each bin is shifted by the same amount. If an array, each bin is shifted independently and the length of bottom must match the number of bins. If None, defaults to 0.

樣例 10:統(tǒng)一的桶初始值
plt.hist(np.random.randn(10000), bottom=-1000)
plt.title("Example 10")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

histype : 多個(gè)數(shù)據(jù)的直方圖類(lèi)型
  • {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}, default: ‘bar’

The type of histogram to draw.

  • ‘bar’ is a traditional bar-type histogram. If multiple data are given the bars are arranged side by side.
  • ‘barstacked’ is a bar-type histogram where multiple data are stacked on top of each other.
  • ‘step’ generates a lineplot that is by default unfilled.
  • ‘stepfilled’ generates a lineplot that is by default filled.
樣例 11:barstacked 類(lèi)型直方圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
         histtype="barstacked")
plt.title("Example 11")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

樣例 12:step 類(lèi)型直方圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
         histtype="step")
plt.title("Example 12")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

樣例 13:stepfilled 類(lèi)型直方圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
         histtype="stepfilled")
plt.title("Example 13")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

align : 對(duì)齊方式
  • {‘left’, ‘mid’, ‘right’}, default: ‘mid’

The horizontal alignment of the histogram bars.

  • ‘left’: bars are centered on the left bin edges.
  • ‘mid’: bars are centered between the bin edges.
  • ‘right’: bars are centered on the right bin edges.
orientation : 橫向 / 縱向柱狀圖
  • {‘vertical’, ‘horizontal’}, default: ‘vertical’

If ‘horizontal’, barh will be used for bar-type histograms and the bottom kwarg will be the left edges.

樣例 14:橫向直方圖
plt.hist(np.random.randn(10000), orientation="horizontal")
plt.title("Example 14")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

rwidth : 條形圖的相對(duì)寬度
  • float or None, default: None

The relative width of the bars as a fraction of the bin width. If None, automatically compute the width.

Ignored if histtype is ‘step’ or ‘stepfilled’.

log : 是否對(duì) y 軸使用對(duì)數(shù)縮放
  • bool, default: False

If True, the histogram axis will be set to a log scale.

color : 桶的顏色
  • color or array-like of colors or None, default: None

Color or sequence of colors, one per dataset. Default (None) uses the standard line color sequence.

label : 標(biāo)簽
  • str or None, default: None

String, or sequence of strings to match multiple datasets. Bar charts yield multiple patches per dataset, but only the first gets the label, so that legend will work as expected.

stacked : 是否繪制堆積圖
  • bool, default: False

If True, multiple data are stacked on top of each other If False multiple data are arranged side by side if histtype is ‘bar’ or on top of each other if histtype is ‘step’.

樣例 15:繪制堆積圖
plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)],
         stacked=True)
plt.title("Example 15")
plt.show()

python 直方圖,Python,# Python 實(shí)例,matplotlib,python,numpy

返回值列表

>>> n, bins, patches = plt.hist([np.random.randn(10000), 2 * np.random.randn(10000), 3 * np.random.randn(5000)])
>>> n
[[0.000e+00 0.000e+00 0.000e+00 6.900e+01 3.325e+03 6.092e+03 5.140e+02
  0.000e+00 0.000e+00 0.000e+00]
 [0.000e+00 2.000e+00 9.700e+01 9.420e+02 3.086e+03 3.797e+03 1.773e+03
  2.830e+02 2.000e+01 0.000e+00]
 [1.000e+01 5.500e+01 2.200e+02 7.170e+02 1.188e+03 1.341e+03 9.130e+02
  4.200e+02 1.130e+02 2.300e+01]]
>>> bins
[-10.7843734   -8.71395108  -6.64352876  -4.57310643  -2.50268411
  -0.43226179   1.63816053   3.70858286   5.77900518   7.8494275
   9.91984982]
>>> patches
<a list of 3 BarContainer objects>
n : 每一個(gè)堆的樣本數(shù)
  • array or list of arrays

The values of the histogram bins. See density and weights for a description of the possible semantics. If input x is an array, then this is an array of length nbins. If input is a sequence of arrays [data1, data2, ...], then this is a list of arrays with the values of the histograms for each of the arrays in the same order. The dtype of the array n (or of its element arrays) will always be float even if no weighting or normalization is used.

bins : 堆的邊界
  • array

The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin). Always a single array even when multiple data sets are passed in.

patches : BarContainer 對(duì)象的列表
  • BarContainer or list of a single Polygon or list of such objects

Container of individual artists used to create the histogram or list of such containers if there are multiple input datasets.文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-733812.html

到了這里,關(guān)于Python 實(shí)例|matplotlib|繪制直方圖(各參數(shù)樣例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀(guān)點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【plt.hist繪制直方圖】:從入門(mén)到精通,只需一篇文章!【Matplotlib可視化】

    【plt.hist繪制直方圖】:從入門(mén)到精通,只需一篇文章!【Matplotlib可視化】

    【??plt.pie繪制直方圖】:從入門(mén)到精通,只需一篇文章!【Matplotlib可視化】! 利用Matplotlib進(jìn)行數(shù)據(jù)可視化示例 ??數(shù)據(jù)可視化是數(shù)據(jù)分析和機(jī)器學(xué)習(xí)領(lǐng)域不可或缺的一部分。其中,直方圖作為一種簡(jiǎn)單而直觀(guān)的數(shù)據(jù)展示方式,常被用于展示數(shù)據(jù)的分布情況。在Python的Mat

    2024年02月22日
    瀏覽(88)
  • Python繪制直方圖

    Python繪制直方圖

    對(duì)于大量樣本來(lái)說(shuō),如果想快速獲知其分布特征,最方便的可視化方案就是直方圖,即統(tǒng)計(jì)落入不同區(qū)間中的樣本個(gè)數(shù)。 以正態(tài)分布為例 其中 bins 參數(shù)用于調(diào)控區(qū)間個(gè)數(shù),出圖結(jié)果如下 直方圖函數(shù)的定義如下 除了 x 和 bins 之外,其他參數(shù)含義為 range 繪圖區(qū)間,默認(rèn)將樣本

    2024年02月05日
    瀏覽(37)
  • python中的matplotlib畫(huà)直方圖(數(shù)據(jù)分析與可視化)

    python中的matplotlib畫(huà)直方圖(數(shù)據(jù)分析與可視化)

    python中的matplotlib畫(huà)直方圖(數(shù)據(jù)分析與可視化) 效果圖: 搞定,這只是一個(gè)小demo,數(shù)據(jù)是代碼生成的,您的數(shù)據(jù)可以從其他地方獲取。照葫蘆畫(huà)瓢。

    2024年02月11日
    瀏覽(27)
  • 【OpenCV ? c++】直方圖計(jì)算 | 繪制 H-S 直方圖 | 繪制一維直方圖 | 繪制 RGB 三色直方圖

    ??直方圖廣泛應(yīng)用于很多計(jì)算機(jī)視覺(jué)處理當(dāng)中。通過(guò)標(biāo)記幀與幀之間顯著的邊緣和顏色的變化,可以檢測(cè)視頻中的場(chǎng)景變化。在每個(gè)興趣點(diǎn)設(shè)置一個(gè)有相似特征的直方圖所構(gòu)成的“標(biāo)簽”,可以用來(lái)標(biāo)記各種不同的事情,比如圖像的色彩分布,物體邊緣梯度模板等等。是計(jì)

    2024年02月09日
    瀏覽(18)
  • python中利用seaborn繪制概率分布直方圖以及密度圖

    python中利用seaborn繪制概率分布直方圖以及密度圖

    當(dāng)我們想要弄清楚變量的統(tǒng)計(jì)特性時(shí),往往想知道它是服從什么分布的,這時(shí)候就需要繪制概率分布直方圖 在python中我們可以使用 seaborn 庫(kù)來(lái)進(jìn)行繪制: Seaborn是一個(gè)基于matplotlib的Python數(shù)據(jù)可視化庫(kù)。它為繪制有吸引力和信息豐富的統(tǒng)計(jì)圖形提供了高級(jí)界面。 首先需要導(dǎo)入

    2024年02月16日
    瀏覽(27)
  • 【matplotlib 實(shí)戰(zhàn)】--直方圖

    【matplotlib 實(shí)戰(zhàn)】--直方圖

    直方圖 ,又稱(chēng)質(zhì)量分布圖,用于表示數(shù)據(jù)的分布情況,是一種常見(jiàn)的統(tǒng)計(jì)圖表。 一般用橫軸表示數(shù)據(jù)區(qū)間,縱軸表示分布情況,柱子越高,則落在該區(qū)間的數(shù)量越大。 構(gòu)建直方圖時(shí),首先首先就是對(duì)數(shù)據(jù)劃分區(qū)間,通俗的說(shuō)即是劃定有幾根柱子(比如,1980年~2020年的數(shù)據(jù),

    2024年02月08日
    瀏覽(20)
  • Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖

    Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖

    根據(jù)箱型圖、直方圖的代碼和數(shù)據(jù)的條件查詢(xún)方法,畫(huà)出航空公司男性和女性用戶(hù)的年齡分布 箱型圖 和 直方圖 。 目錄 ?圖形簡(jiǎn)介 1. 箱線(xiàn)圖 2.直方圖 引入模塊 獲取數(shù)據(jù) 處理數(shù)據(jù) 根據(jù)性別來(lái)分開(kāi)查詢(xún)數(shù)據(jù) 畫(huà)圖 箱型圖 ?直方圖 男性直方圖 1. 箱線(xiàn)圖 箱線(xiàn)圖(Box-plot)又稱(chēng)為

    2024年02月05日
    瀏覽(18)
  • Matplotlib可視化數(shù)據(jù)分析圖表下(常用圖表的繪制、折線(xiàn)圖、柱形圖、直方圖、餅形圖、散點(diǎn)圖、面積圖、熱力圖、箱形圖、3D圖表、繪制多個(gè)圖表、雙y軸可視化圖表、顏色漸變圖)

    Matplotlib可視化數(shù)據(jù)分析圖表下(常用圖表的繪制、折線(xiàn)圖、柱形圖、直方圖、餅形圖、散點(diǎn)圖、面積圖、熱力圖、箱形圖、3D圖表、繪制多個(gè)圖表、雙y軸可視化圖表、顏色漸變圖)

    本文來(lái)自《Python數(shù)據(jù)分析從入門(mén)到精通》_明日科技編著 本節(jié)介紹常用圖表的繪制,主要包括繪制折線(xiàn)圖、繪制柱形圖、繪制直方圖、繪制餅形圖、繪制散點(diǎn)圖、繪制面積圖、繪制熱力圖、繪制箱型圖、繪制3D圖表、繪制多個(gè)子圖表以及圖表的保存。對(duì)于常用的圖表類(lèi)型以繪制

    2023年04月23日
    瀏覽(43)
  • python數(shù)據(jù)可視化玩轉(zhuǎn)Matplotlib直方圖、箱型圖、密度圖、正態(tài)分布、偏度和峰度

    python數(shù)據(jù)可視化玩轉(zhuǎn)Matplotlib直方圖、箱型圖、密度圖、正態(tài)分布、偏度和峰度

    目錄 1. 直方圖、箱線(xiàn)圖和密度圖 1.1 直方圖 1.2 箱線(xiàn)圖 1.3 密度圖 2. 正態(tài)分布 3. 偏度和峰度 結(jié)論 直方圖、箱線(xiàn)圖和密度圖是數(shù)據(jù)分析中十分常用的圖形。它們可以幫助我們更好地理解數(shù)據(jù)的分布情況,從而更好地進(jìn)行數(shù)據(jù)分析和處理。在這篇博客中,我們將介紹它們的基本

    2024年02月09日
    瀏覽(21)
  • Python從零到壹丨帶你了解圖像直方圖理論知識(shí)和繪制實(shí)現(xiàn)

    摘要: 本文將從OpenCV和Matplotlib兩個(gè)方面介紹如何繪制直方圖,這將為圖像處理像素對(duì)比提供有效支撐。 本文分享自華為云社區(qū)《[Python從零到壹] 五十.圖像增強(qiáng)及運(yùn)算篇之圖像直方圖理論知識(shí)和繪制實(shí)現(xiàn)》,作者:eastmount。 灰度直方圖是灰度級(jí)的函數(shù),描述的是圖像中每種

    2024年02月05日
    瀏覽(26)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包