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

matplotlib中plt.hist()參數(shù)解釋及應用實例

 更新時間:2022年08月15日 15:43:08   作者:hengheng21  
本文主要介紹了matplotlib中plt.hist()參數(shù)解釋及應用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、plt.hist()參數(shù)詳解

簡介:
plt.hist():直方圖,一種特殊的柱狀圖。
將統(tǒng)計值的范圍分段,即將整個值的范圍分成一系列間隔,然后計算每個間隔中有多少值。
直方圖也可以被歸一化以顯示“相對”頻率。 然后,它顯示了屬于幾個類別中的每個類別的占比,其高度總和等于1。

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib import ticker
%matplotlib inline


plt.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)

常用參數(shù)解釋:
x: 作直方圖所要用的數(shù)據,必須是一維數(shù)組;多維數(shù)組可以先進行扁平化再作圖;必選參數(shù);
bins: 直方圖的柱數(shù),即要分的組數(shù),默認為10;
range:元組(tuple)或None;剔除較大和較小的離群值,給出全局范圍;如果為None,則默認為(x.min(), x.max());即x軸的范圍;
density:布爾值。如果為true,則返回的元組的第一個參數(shù)n將為頻率而非默認的頻數(shù);
weights:與x形狀相同的權重數(shù)組;將x中的每個元素乘以對應權重值再計數(shù);如果normed或density取值為True,則會對權重進行歸一化處理。這個參數(shù)可用于繪制已合并的數(shù)據的直方圖;
cumulative:布爾值;如果為True,則計算累計頻數(shù);如果normed或density取值為True,則計算累計頻率;
bottom:數(shù)組,標量值或None;每個柱子底部相對于y=0的位置。如果是標量值,則每個柱子相對于y=0向上/向下的偏移量相同。如果是數(shù)組,則根據數(shù)組元素取值移動對應的柱子;即直方圖上下便宜距離;
histtype:{‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’};'bar’是傳統(tǒng)的條形直方圖;'barstacked’是堆疊的條形直方圖;'step’是未填充的條形直方圖,只有外邊框;‘stepfilled’是有填充的直方圖;當histtype取值為’step’或’stepfilled’,rwidth設置失效,即不能指定柱子之間的間隔,默認連接在一起;
align:{‘left’, ‘mid’, ‘right’};‘left’:柱子的中心位于bins的左邊緣;‘mid’:柱子位于bins左右邊緣之間;‘right’:柱子的中心位于bins的右邊緣;
orientation:{‘horizontal’, ‘vertical’}:如果取值為horizontal,則條形圖將以y軸為基線,水平排列;簡單理解為類似bar()轉換成barh(),旋轉90°;
rwidth:標量值或None。柱子的寬度占bins寬的比例;
log:布爾值。如果取值為True,則坐標軸的刻度為對數(shù)刻度;如果log為True且x是一維數(shù)組,則計數(shù)為0的取值將被剔除,僅返回非空的(frequency, bins, patches);
color:具體顏色,數(shù)組(元素為顏色)或None。
label:字符串(序列)或None;有多個數(shù)據集時,用label參數(shù)做標注區(qū)分;
stacked:布爾值。如果取值為True,則輸出的圖為多個數(shù)據集堆疊累計的結果;如果取值為False且histtype=‘bar’或’step’,則多個數(shù)據集的柱子并排排列;
normed: 是否將得到的直方圖向量歸一化,即顯示占比,默認為0,不歸一化;不推薦使用,建議改用density參數(shù);
edgecolor: 直方圖邊框顏色;
alpha: 透明度;

返回值(用參數(shù)接收返回值,便于設置數(shù)據標簽):
n:直方圖向量,即每個分組下的統(tǒng)計值,是否歸一化由參數(shù)normed設定。當normed取默認值時,n即為直方圖各組內元素的數(shù)量(各組頻數(shù));
bins: 返回各個bin的區(qū)間范圍;
patches:返回每個bin里面包含的數(shù)據,是一個list。
其他參數(shù)與plt.bar()類似。

二、plt.hist()簡單應用

import matplotlib.pyplot as plt
%matplotlib inline


# 最簡單,只傳遞x,組數(shù),寬度,范圍
plt.hist(data13['carrier_no'], bins=11, rwidth=0.8, range=(1,12), align='left')
plt.show()

三、plt.bar()綜合應用

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib import ticker
%matplotlib inline


plt.figure(figsize=(8,5), dpi=80)
# 拿參數(shù)接收hist返回值,主要用于記錄分組返回的值,標記數(shù)據標簽
n, bins, patches = plt.hist(data13['carrier_no'], bins=11, rwidth=0.8, range=(1,12), align='left', label='xx直方圖')
for i in range(len(n)):
    plt.text(bins[i], n[i]*1.02, int(n[i]), fontsize=12, horizontalalignment="center") #打標簽,在合適的位置標注每個直方圖上面樣本數(shù)
plt.ylim(0,16000)
plt.title('直方圖')
plt.legend()
# plt.savefig('直方圖'+'.png')
plt.show()

附官方參數(shù)解釋

Parameters
----------
x : (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.

bins : int or sequence or str, optional
    If an integer is given, ``bins + 1`` bin edges are calculated and
    returned, consistent with `numpy.histogram`.

    If `bins` is a sequence, gives bin edges, including left edge of
    first bin and right edge of last bin.  In this case, `bins` is
    returned unmodified.

    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.

    Unequally spaced bins are supported if *bins* is a sequence.

    With Numpy 1.11 or newer, you can alternatively provide a string
    describing a binning strategy, such as 'auto', 'sturges', 'fd',
    'doane', 'scott', 'rice' or 'sqrt', see
    `numpy.histogram`.

    The default is taken from :rc:`hist.bins`.

range : tuple or None, optional
    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.

    Default is ``None``

density : bool, optional
    If ``True``, the first element of the return tuple will
    be the counts normalized to form a probability density, i.e.,
    the area (or integral) under the histogram will sum to 1.
    This is achieved by dividing the count by the number of
    observations times the bin width and not dividing by the total
    number of observations. If *stacked* is also ``True``, the sum of
    the histograms is normalized to 1.

    Default is ``None`` for both *normed* and *density*. If either is
    set, then that value will be used. If neither are set, then the
    args will be treated as ``False``.

    If both *density* and *normed* are set an error is raised.

weights : (n, ) array_like or None, optional
    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 *normed* or *density* is ``True``,
    the weights are normalized, so that the integral of the density
    over the range remains 1.

    Default is ``None``.

    This parameter can be used to draw a histogram of data that has
    already been binned, e.g. using `np.histogram` (by treating each
    bin as a single point with a weight equal to its count) ::

        counts, bins = np.histogram(data)
        plt.hist(bins[:-1], bins, weights=counts)

    (or you may alternatively use `~.bar()`).

cumulative : bool, optional
    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 *normed* or *density*
    is also ``True`` then the histogram is normalized such that the
    last bin equals 1. If *cumulative* evaluates to less than 0
    (e.g., -1), the direction of accumulation is reversed.
    In this case, if *normed* and/or *density* is also ``True``, then
    the histogram is normalized such that the first bin equals 1.

    Default is ``False``

bottom : array_like, scalar, or None
    Location of the bottom baseline of each bin.  If a scalar,
    the base line for 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.

    Default is ``None``

histtype : {'bar', 'barstacked', 'step',  'stepfilled'}, optional
    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.

    Default is 'bar'

align : {'left', 'mid', 'right'}, optional
    Controls how the histogram is plotted.

        - '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.

    Default is 'mid'

orientation : {'horizontal', 'vertical'}, optional
    If 'horizontal', `~matplotlib.pyplot.barh` will be used for
    bar-type histograms and the *bottom* kwarg will be the left edges.

rwidth : scalar or None, optional
    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'.

    Default is ``None``

log : bool, optional
    If ``True``, the histogram axis will be set to a log scale. If
    *log* is ``True`` and *x* is a 1D array, empty bins will be
    filtered out and only the non-empty ``(n, bins, patches)``
    will be returned.

    Default is ``False``

color : color or array_like of colors or None, optional
    Color spec or sequence of color specs, one per dataset.  Default
    (``None``) uses the standard line color sequence.

    Default is ``None``

label : str or None, optional
    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 the legend command will work as expected.

    default is ``None``

stacked : bool, optional
    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'

    Default is ``False``

normed : bool, optional
    Deprecated; use the density keyword argument instead.

Returns
-------
n : 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 : list or list of lists
    Silent list of individual patches used to create the histogram
    or list of such list if multiple input datasets.

Other Parameters
----------------
**kwargs : `~matplotlib.patches.Patch` properties

See also
--------
hist2d : 2D histograms

Notes
-----


.. note::
    In addition to the above described arguments, this function can take a
    **data** keyword argument. If such a **data** argument is given, the
    following arguments are replaced by **data[<arg>]**:

    * All arguments with the following names: 'weights', 'x'.

    Objects passed as **data** must support item access (``data[<arg>]``) and
    membership test (``<arg> in data``).

到此這篇關于matplotlib中plt.hist()參數(shù)解釋及應用實例的文章就介紹到這了,更多相關matplotlib plt.hist()參數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python自定義線程池控制線程數(shù)量的示例

    python自定義線程池控制線程數(shù)量的示例

    今天小編就為大家分享一篇python自定義線程池控制線程數(shù)量的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • 淺談Python的條件判斷語句if/else語句

    淺談Python的條件判斷語句if/else語句

    這篇文章主要介紹了Python的條件判斷語句if/else語句,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • pyinstaller打包程序exe踩過的坑

    pyinstaller打包程序exe踩過的坑

    這篇文章主要介紹了pyinstaller打包exe踩過的坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Python實現(xiàn)在線程里運行scrapy的方法

    Python實現(xiàn)在線程里運行scrapy的方法

    這篇文章主要介紹了Python實現(xiàn)在線程里運行scrapy的方法,涉及Python線程操作的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Python列表對象中元素的刪除操作方法

    Python列表對象中元素的刪除操作方法

    列表的刪操作指的是在列表中刪除已存在的元素,列表中的元素被刪除后,后面所有的元素依次往前移動一位,掛在被刪除元素的索引下,保證每一個索引都有元素,這篇文章主要介紹了Python列表對象中元素的刪除操作方法,需要的朋友可以參考下
    2022-12-12
  • 基于Python實現(xiàn)繪制簡單動圖的示例詳解

    基于Python實現(xiàn)繪制簡單動圖的示例詳解

    動畫是一種高效的可視化工具,能夠提升用戶的吸引力和視覺體驗,有助于以富有意義的方式呈現(xiàn)數(shù)據可視化,本文的主要介紹在Python中兩種簡單制作動圖的方法,需要的可以了解下
    2023-10-10
  • python測試攻略pytest.main()隱藏利器實例探究

    python測試攻略pytest.main()隱藏利器實例探究

    在Pytest測試框架中,pytest.main()是一個重要的功能,用于啟動測試執(zhí)行,它允許以不同方式運行測試,傳遞參數(shù)和配置選項,本文將深入探討pytest.main()的核心功能,提供豐富的示例代碼和更全面的內容,
    2024-01-01
  • python自動計算圖像數(shù)據集的RGB均值

    python自動計算圖像數(shù)據集的RGB均值

    這篇文章主要為大家詳細介紹了python自動計算圖像數(shù)據集的RGB均值,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Python實現(xiàn)簡單的

    Python實現(xiàn)簡單的"導彈" 自動追蹤原理解析

    這篇文章主要介紹了Python實現(xiàn)簡單的"導彈" 自動追蹤原理解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • python定時執(zhí)行指定函數(shù)的方法

    python定時執(zhí)行指定函數(shù)的方法

    這篇文章主要介紹了python定時執(zhí)行指定函數(shù)的方法,涉及Python中sleep方法延時執(zhí)行的相關使用技巧,需要的朋友可以參考下
    2015-05-05

最新評論

茶陵县| 伊宁市| 常山县| 从江县| 定西市| 炉霍县| 从江县| 牡丹江市| 奉新县| 宜都市| 房山区| 三穗县| 微博| 罗江县| 隆化县| 连平县| 石楼县| 襄垣县| 宁国市| 团风县| 托克逊县| 周至县| 盘山县| 通州市| 通城县| 宁德市| 依兰县| 余江县| 同江市| 根河市| 乌海市| 策勒县| 甘泉县| 赣州市| 长白| 齐河县| 英山县| SHOW| 石嘴山市| 密山市| 上犹县|