iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >matplotlib中plt.hist()参数解释及应用实例
  • 598
分享到

matplotlib中plt.hist()参数解释及应用实例

2024-04-02 19:04:59 598人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、plt.hist()参数详解二、plt.hist()简单应用三、plt.bar()综合应用附官方参数解释一、plt.hist()参数详解 简介:plt.hist():直方图,

一、plt.hist()参数详解

简介:
plt.hist():直方图,一种特殊的柱状图。
将统计值的范围分段,即将整个值的范围分成一系列间隔,然后计算每个间隔中有多少值。
直方图也可以被归一化以显示“相对”频率。 然后,它显示了属于几个类别中的每个类别的占比,其高度总和等于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)

常用参数解释:
x: 作直方图所要用的数据,必须是一维数组;多维数组可以先进行扁平化再作图;必选参数;
bins: 直方图的柱数,即要分的组数,默认为10;
range:元组(tuple)或None;剔除较大和较小的离群值,给出全局范围;如果为None,则默认为(x.min(), x.max());即x轴的范围;
density:布尔值。如果为true,则返回的元组的第一个参数n将为频率而非默认的频数;
weights:与x形状相同的权重数组;将x中的每个元素乘以对应权重值再计数;如果normed或density取值为True,则会对权重进行归一化处理。这个参数可用于绘制已合并的数据的直方图;
cumulative:布尔值;如果为True,则计算累计频数;如果normed或density取值为True,则计算累计频率;
bottom:数组,标量值或None;每个柱子底部相对于y=0的位置。如果是标量值,则每个柱子相对于y=0向上/向下的偏移量相同。如果是数组,则根据数组元素取值移动对应的柱子;即直方图上下便宜距离;
histtype:{‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’};'bar’是传统的条形直方图;'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,则坐标轴的刻度为对数刻度;如果log为True且x是一维数组,则计数为0的取值将被剔除,仅返回非空的(frequency, bins, patches);
color:具体颜色,数组(元素为颜色)或None。
label:字符串(序列)或None;有多个数据集时,用label参数做标注区分;
stacked:布尔值。如果取值为True,则输出的图为多个数据集堆叠累计的结果;如果取值为False且histtype=‘bar’或’step’,则多个数据集的柱子并排排列;
normed: 是否将得到的直方图向量归一化,即显示占比,默认为0,不归一化;不推荐使用,建议改用density参数;
edgecolor: 直方图边框颜色;
alpha: 透明度;

返回值(用参数接收返回值,便于设置数据标签):
n:直方图向量,即每个分组下的统计值,是否归一化由参数normed设定。当normed取默认值时,n即为直方图各组内元素的数量(各组频数);
bins: 返回各个bin的区间范围;
patches:返回每个bin里面包含的数据,是一个list。
其他参数与plt.bar()类似。

二、plt.hist()简单应用

import matplotlib.pyplot as plt
%matplotlib inline


# 最简单,只传递x,组数,宽度,范围
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)
# 拿参数接收hist返回值,主要用于记录分组返回的值,标记数据标签
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") #打标签,在合适的位置标注每个直方图上面样本数
plt.ylim(0,16000)
plt.title('直方图')
plt.legend()
# plt.savefig('直方图'+'.png')
plt.show()

附官方参数解释

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()参数解释及应用实例的文章就介绍到这了,更多相关matplotlib plt.hist()参数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: matplotlib中plt.hist()参数解释及应用实例

本文链接: https://www.lsjlt.com/news/119989.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • matplotlib中plt.hist()参数解释及应用实例
    目录一、plt.hist()参数详解二、plt.hist()简单应用三、plt.bar()综合应用附官方参数解释一、plt.hist()参数详解 简介:plt.hist():直方图,...
    99+
    2024-04-02
  • Python中np.random.randint()参数详解及用法实例
    目录可实现功能:np.random.randint() 根据参数中所指定的范围生成随机 整数。参数一、基础用法二、高级用法总结可实现功能: 1.随机生成一个整数。 2.随机生成任意范...
    99+
    2024-04-02
  • Python中函数定义及参数实例
    1.函数定义     函数就是完成特定功能的一个语句组,这组语句可以作为一个单位使用,并且给它取一个名字 ,可以通过函数名在程序的不同地方多次执行(这通常叫函数调用)预定义函数(可以直接使用)自定义函数(自己编写)为什么使用函数?    降...
    99+
    2023-01-31
    函数 实例 定义
  • sklearn交叉验证函数cross_val_score用法及参数解释
    文章目录 一 、使用示例二、参数含义三、常见的scoring取值1.分类、回归和聚类scoring参数选择2.f1_micro和f1_macro区别3.负均方误差和均方误差 一 、使用...
    99+
    2023-09-07
    sklearn 机器学习 python
  • python中matplotlib的颜色以及形状实例详解
    目录绘制折线图绘制柱形图簇状柱形图堆积柱形图散点图附:matplotlib实现区域颜色填充总结 绘制折线图 命令形如: # 常用 plt.plot(x, y, linewi...
    99+
    2024-04-02
  • 深入解析Python命令行参数并实例应用
    Python命令行参数详解及实例应用 在Python编程中,我们经常会需要从命令行中获取参数来执行不同的操作。Python内置了一个argparse模块,可以帮助我们解析命令行参数,并根据参数执行不同的逻辑。本文将详细介绍arg...
    99+
    2024-02-03
    实例应用 参数详解 标准库
  • JVM中生产环境参数实例及分析
    今天就跟大家聊聊有关JVM中生产环境参数实例及分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。java application项目(非web项目)改进前:-Xms128m ...
    99+
    2023-06-17
  • C++ 可变参数的实际应用案例
    可变参数函数允许函数接受任意数量的参数,可用于处理未知数量的输入。例如,可声明一个函数计算数组中最大值:声明可变参数函数 max,接收一个整型参数 num 和可变参数 ...。初始化 v...
    99+
    2024-04-19
    c++ 可变参数
  • Go语言输入函数详解及应用实例
    Go语言输入函数详解及应用实例 Go语言作为一门开发效率高、易于学习的现代编程语言,其标准库中提供了丰富的输入函数,方便开发者进行输入数据的处理。本文将详细介绍Go语言中常用的输入函数...
    99+
    2024-04-02
  • Java中的隐式参数和显示参数实例详解
            在学习Java的过程中,我们会遇到许多的问题。下面我们就来看看什么是隐式参数和显示参数。     &nb...
    99+
    2023-05-31
    java 隐式参数 显示参数
  • python open函数中newline参数实例详解
    目录问题的由来具体实例总结问题的由来 我在读pythoncsv模块文档 看到了这样一句话 如果 csvfile 是文件对象,则打开它时应使用 newline=‘&rsqu...
    99+
    2024-04-02
  • jquery中ajax方法的参数属性及作用实例分析
    这篇“jquery中ajax方法的参数属性及作用实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起...
    99+
    2024-04-02
  • Python中super()的理解以及应用场景实例
    目录一、前言二、什么是super三、super的常用使用场景总结一、前言 最近有粉丝向我咨询super相关的问题,说网上搜索到的教程不够通俗易懂,看了之后还是不太理解。所以在这里基...
    99+
    2024-04-02
  • Echarts中常用的参数总结及参数自定义示例代码
    目录前言折线图1.title2.tooltip3.grid4.legend5.xAxis6.yAxis7.series附:常见问题1、自定义X轴文字(文字替换)2、自定义X轴文字(文...
    99+
    2023-02-18
    echarts 参数 echarts怎么用 echarts 参数自定义
  • MySQL中EXPLAIN解释命令及用法的示例分析
    这篇文章将为大家详细讲解有关MySQL中EXPLAIN解释命令及用法的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1,情景描述:同事教我在mysql中用expl...
    99+
    2024-04-02
  • Python脚本开发中的命令行参数及传参示例详解
    目录sys模块argparse模块Python中的正则表达式 正则表达式简介Re模块常用的匹配规则sys模块 在使用python开发脚本的时候,作为一个运维工具,或者是其他...
    99+
    2024-04-02
  • SpringMVC中参数绑定问题实例详解
    目录前言1. 参数绑定的过程2. 默认支持的类型3. 简单类型的绑定4. pojo类型的绑定4.1 普通pojo类型4.2. 包装的pojo类型5. 集合类型的绑定5.1 数组的绑定...
    99+
    2024-04-02
  • vue中使用window.open()参数示例详解
    目录vue中使用window.open()参数详解1.window.open()有三个参数2.举例说明 3.第二个参数详解 4.第三个参数详解 补充:在...
    99+
    2023-05-17
    vue window.open()参数 windom.open()使用
  • PHPcreate_function()函数应用实例详解
    目录前言create_function()简介函数功能代码注入实例0x010x02前言 一直遇到过这个 函数,但是不知道怎么利用 这回学习一下这个函数 creat...
    99+
    2024-04-02
  • 正则表达式预查的详细解释与应用实例
    目录解释应用难点-为什么.[^#/n].*不行总结 解释 预查又称环视(Lookaround)、属于零宽断言(Zero-Length Assertions)的一种,有 4 ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作