Matplotlib(3、直方图) – plt.hist()参数解释&应用实例

matplotlib画直方图 – 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

plt.hist(data13['carrier_no'], bins=11, rwidth=0.8, range=(1,12), align='left')
plt.show()

Matplotlib(3、直方图) - plt.hist()参数解释&应用实例

三、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)

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.show()

Matplotlib(3、直方图) - plt.hist()参数解释&应用实例

官方参数解释

Parameters 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</code>[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
hist2d : 2D histograms

Notes

Original: https://blog.csdn.net/weixin_45520028/article/details/113924866
Author: hengheng21
Title: Matplotlib(3、直方图) - plt.hist()参数解释&应用实例

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/764079/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球