Python 实例|matplotlib|绘制直方图(各参数样例)

这篇具有很好参考价值的文章主要介绍了Python 实例|matplotlib|绘制直方图(各参数样例)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

matplotlib.pyplot.hist

这个方法使用 numpy.histogram 首先将 x 中的数据分桶并统计每个桶中的元素数量,接着使用条形图绘制这个分布。

函数参数、含义及样例如下:

from matplotlib import pyplot as plt
import numpy as np

参数列表及样例

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.

样例 1:当 x 为多维向量时,绘制多维向量的直方图
plt.hist(np.random.randn(10000))
plt.title("Example 1")
plt.show()

python 直方图,Python,# Python 实例,matplotlib,python,numpy

样例 2:当 x 为 m 个多维向量的列表时(各个向量的长度不需要全部相同),将在同一个轴上分别绘制 m 个多维向量的直方图
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 实例,matplotlib,python,numpy

bins : 分桶数 / 分桶数值(非必填)
  • 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: ‘auto’, ‘fd’, ‘doane’, ‘scott’, ‘stone’, ‘rice’, ‘sturges’, or ‘sqrt’.

此参数用于传给 numpy.histogrambins 形参,详情见 numpy.histogram_bin_edges 的文档。

样例 3:当 bins 传入整数时,会分为 bins 数量的等宽桶
plt.hist(np.random.randn(10000), bins=20)
plt.title("Example 3")
plt.show()

python 直方图,Python,# Python 实例,matplotlib,python,numpy

样例 4:当 bins 传入序列时,会视作每个桶的边界
plt.hist(np.random.randn(10000), bins=[-3, -0.8, 0, 0.3, 3])
plt.title("Example 4")
plt.show()

python 直方图,Python,# Python 实例,matplotlib,python,numpy

样例 5:当传入字符串时,例如 “fd
plt.hist(np.random.randn(10000), bins="fd")
plt.title("Example 5")
plt.show()

python 直方图,Python,# Python 实例,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.

此参数用于传给 numpy.histogramrange 形参,详情见 numpy.histogram_bin_edges 的文档。

样例 6:使用 range 参数
plt.hist(np.random.randn(10000), range=(0, 1))
plt.title("Example 6")
plt.show()

python 直方图,Python,# Python 实例,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 实例,matplotlib,python,numpy

weight :是否为元素赋予权重
  • (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:配置权重
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 实例,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 实例,matplotlib,python,numpy

bottom : 每个桶的初始值
  • 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:统一的桶初始值
plt.hist(np.random.randn(10000), bottom=-1000)
plt.title("Example 10")
plt.show()

python 直方图,Python,# Python 实例,matplotlib,python,numpy

histype : 多个数据的直方图类型
  • {‘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 类型直方图
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 实例,matplotlib,python,numpy

样例 12:step 类型直方图
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 实例,matplotlib,python,numpy

样例 13:stepfilled 类型直方图
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 实例,matplotlib,python,numpy

align : 对齐方式
  • {‘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 实例,matplotlib,python,numpy

rwidth : 条形图的相对宽度
  • 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 : 是否对 y 轴使用对数缩放
  • 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 : 标签
  • 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 实例,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 : 每一个堆的样本数
  • 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 对象的列表
  • 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.文章来源地址https://www.toymoban.com/news/detail-733812.html

到了这里,关于Python 实例|matplotlib|绘制直方图(各参数样例)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【plt.hist绘制直方图】:从入门到精通,只需一篇文章!【Matplotlib可视化】

    【📊plt.pie绘制直方图】:从入门到精通,只需一篇文章!【Matplotlib可视化】! 利用Matplotlib进行数据可视化示例   数据可视化是数据分析和机器学习领域不可或缺的一部分。其中,直方图作为一种简单而直观的数据展示方式,常被用于展示数据的分布情况。在Python的Mat

    2024年02月22日
    浏览(31)
  • Python绘制直方图

    对于大量样本来说,如果想快速获知其分布特征,最方便的可视化方案就是直方图,即统计落入不同区间中的样本个数。 以正态分布为例 其中 bins 参数用于调控区间个数,出图结果如下 直方图函数的定义如下 除了 x 和 bins 之外,其他参数含义为 range 绘图区间,默认将样本

    2024年02月05日
    浏览(29)
  • python中的matplotlib画直方图(数据分析与可视化)

    python中的matplotlib画直方图(数据分析与可视化) 效果图: 搞定,这只是一个小demo,数据是代码生成的,您的数据可以从其他地方获取。照葫芦画瓢。

    2024年02月11日
    浏览(38)
  • 【OpenCV • c++】直方图计算 | 绘制 H-S 直方图 | 绘制一维直方图 | 绘制 RGB 三色直方图

      直方图广泛应用于很多计算机视觉处理当中。通过标记帧与帧之间显著的边缘和颜色的变化,可以检测视频中的场景变化。在每个兴趣点设置一个有相似特征的直方图所构成的“标签”,可以用来标记各种不同的事情,比如图像的色彩分布,物体边缘梯度模板等等。是计

    2024年02月09日
    浏览(37)
  • python中利用seaborn绘制概率分布直方图以及密度图

    当我们想要弄清楚变量的统计特性时,往往想知道它是服从什么分布的,这时候就需要绘制概率分布直方图 在python中我们可以使用 seaborn 库来进行绘制: Seaborn是一个基于matplotlib的Python数据可视化库。它为绘制有吸引力和信息丰富的统计图形提供了高级界面。 首先需要导入

    2024年02月16日
    浏览(42)
  • 【matplotlib 实战】--直方图

    直方图 ,又称质量分布图,用于表示数据的分布情况,是一种常见的统计图表。 一般用横轴表示数据区间,纵轴表示分布情况,柱子越高,则落在该区间的数量越大。 构建直方图时,首先首先就是对数据划分区间,通俗的说即是划定有几根柱子(比如,1980年~2020年的数据,

    2024年02月08日
    浏览(35)
  • Python获取excel的数据并绘制箱型图和直方图

    根据箱型图、直方图的代码和数据的条件查询方法,画出航空公司男性和女性用户的年龄分布 箱型图 和 直方图 。 目录  图形简介 1. 箱线图 2.直方图 引入模块 获取数据 处理数据 根据性别来分开查询数据 画图 箱型图  直方图 男性直方图 1. 箱线图 箱线图(Box-plot)又称为

    2024年02月05日
    浏览(35)
  • Matplotlib可视化数据分析图表下(常用图表的绘制、折线图、柱形图、直方图、饼形图、散点图、面积图、热力图、箱形图、3D图表、绘制多个图表、双y轴可视化图表、颜色渐变图)

    本文来自《Python数据分析从入门到精通》_明日科技编著 本节介绍常用图表的绘制,主要包括绘制折线图、绘制柱形图、绘制直方图、绘制饼形图、绘制散点图、绘制面积图、绘制热力图、绘制箱型图、绘制3D图表、绘制多个子图表以及图表的保存。对于常用的图表类型以绘制

    2023年04月23日
    浏览(37)
  • python数据可视化玩转Matplotlib直方图、箱型图、密度图、正态分布、偏度和峰度

    目录 1. 直方图、箱线图和密度图 1.1 直方图 1.2 箱线图 1.3 密度图 2. 正态分布 3. 偏度和峰度 结论 直方图、箱线图和密度图是数据分析中十分常用的图形。它们可以帮助我们更好地理解数据的分布情况,从而更好地进行数据分析和处理。在这篇博客中,我们将介绍它们的基本

    2024年02月09日
    浏览(36)
  • Python从零到壹丨带你了解图像直方图理论知识和绘制实现

    摘要: 本文将从OpenCV和Matplotlib两个方面介绍如何绘制直方图,这将为图像处理像素对比提供有效支撑。 本文分享自华为云社区《[Python从零到壹] 五十.图像增强及运算篇之图像直方图理论知识和绘制实现》,作者:eastmount。 灰度直方图是灰度级的函数,描述的是图像中每种

    2024年02月05日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包