美文网首页
matplotlib.pyplot.hist绘制直方图

matplotlib.pyplot.hist绘制直方图

作者: 微笑life | 来源:发表于2020-04-05 11:05 被阅读0次

第一:导入包

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

第二:造数据

d = np.random.randn(10000) #生成一个服从高斯分布的10000个样本的数据

第三:参数解释

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,)
3.1 X:是用来绘制图形的数据,即x轴的数据
3.2 bins:可以为整数,也可以为一个序列(比如list)
(1) 当bin为整数时,则等于柱子的个数,有bin + 1个边。
(2)当bin为sequence时,即给定了每个柱子的边界值,柱子个数等于len(sequence) - 1,每个区间,为前闭后开([)),但是最后一个区间为前后闭,如bin = [1, 2, 3, 4],则区间分别为[1,2), [2,3),[3,4]

bins=20
bins=[-4,-3,-2,-1,0,1,2,3,4]

(3)在Numpy >= 1.11时,bin可以为string, 'auto', 'sturges', 'fd','doane', 'scott', 'rice', 'sturges' or 'sqrt'
range: 为bin的最小值和最大值显示区间,但是当bin为sequence时,不起作用,即只对bin=int时起作用。
3.3 edgecolor:是柱子边界的颜色。
3.4 facecolor: 是柱子的颜色。
3.5 color:是柱子的颜色,color和facecolor指定一个即可,如果两者都指定,则color不起作用,而facecolor起作用。且默认颜色为蓝色。
3.6 range: 即对做hist的x的范围进行限定,默认的range=(x.min(), x.max())

range = (-2,2))
range = (-2,16)
3.7 density: 返回数组值的第一个值是以频数展(density=False)示还是频率(density = True)展示。当以频率进行展示时,the counts normalized to form a probability density,即频数被均一化成概率密度形式,柱状图下面的面积为1,公式为频数(count) / (观测总数 * bin的宽度),如果 stacked也=True,则柱状图不被均一化为1.
在新的版本中normed被取消,用density代替, 二者不能同时使用,会报错:ValueError: kwargs 'density' and 'normed' cannot be used simultaneously. Please only use 'density', since 'normed'is deprecated.,如果使用normed会报警告:The 'normed' kwarg was deprecated in Matplotlib 2.1 and will be removed in 3.1. Use 'density' instead.
alternative="'density'", removal="3.1")

density=True的返回值表现
density=True的图形表现
3.8 histtype:柱子的格式,有'bar', 'barstacked', 'step', 'stepfilled'种,bar为默认参数(为传统的bar格式), barstacked 也为bar格式,当数据为1个时,和bar结果一样,当数据为多个时,则进行垂直堆叠,step:为线状的lineplot,类似于没有填充,stepfilled则有填充,和bar效果一致。
histtype = step
3.9 align:align : {'left', 'mid', 'right'}, optional
Controls how the histogram is plotted.
    - 'left': bars are centered on the left bin edges.
    left:柱子的中心位于bin的左边缘处
    - 'mid': bars are centered between the bin edges.
   mid:柱子的中心位于bin的左右边缘的中间,即bin的中心
    - 'right': bars are centered on the right bin edges.
   right:柱子的中心位于bin的右边缘处。
Default is 'mid'
align=left
align = right
3.10 log : bool,默认False,即y坐标轴是否选择指数刻度
log=True
3.11 stacked: bool,默认为False,是否为堆积状图,如图所示,其中a和b数据最高值均为0.4左右,只是堆积在一起,就会把第一个数据a给相对缩小。
stacked = True

3.12 orientation 柱子的方向,垂直(vertical, 默认)和水平(horizontal)

orientation="horizontal"
**3.13 rwidth ** 柱子的相对宽度
rwidth=0.1
rwidth=0.3
rwidth=0.8
rwidth=1.0
3.14 label 数据的标签,用于展示图例时使用。
一组数据 label = "ab" d = np.random.randn(10000)
二组数据 label = ["a", "b"] d = np.random.randn(10000, 2)

3.15 bottom :是指基线的每个bin的相对位置,默认在0.0的位置,即不做任何上下偏移,当作偏移时,可以使bottom的值不为0.0

#tottom的个数要和bins的个数相同。
plt.hist(d, bins=20,density = True, align = "mid",histtype = "bar", log = False, orientation="vertical", rwidth = 1.0, label = ["a", "b"], stacked = True, bottom = [0.1] *20)
bottom = [0.1] *20
bottom = None,默认值
3.16 cumulative :是指结果中是否以累积频数或频率进行展示,默认是不以频率或频数进行展示,即展示各个bin上频数或频率。
plt.hist(d, bins=20, density = True, align = "mid",histtype = "bar", log = False, orientation="vertical", rwidth = 1.0, label = ["a", "b"], stacked = True, cumulative = True)
plt.hist(d, bins=20, density = False, align = "mid",histtype = "bar", log = False, orientation="vertical", rwidth = 1.0, label = ["a", "b"], stacked = True, cumulative = True)
density = True和cumulative = True
density = False和cumulative = True 2组数据

第四:返回值

返回值为一个包含3个元素的tuple
第一个值:每个bin的频率(density = True)或频数(density= False)
第二个值:所有bin的边界值,值的个数为bin_num + 1
可以利用第一和第二返回值进行绘制曲线拟合
第三个值:图形的对象。Patches <a list of 2 Lists of Patches objects>

Patches 1个数据 返回值
2个数据 返回值

第五:实例(官网)

[https://matplotlib.org/gallery/pyplots/pyplot_text.html#sphx-glr-gallery-pyplots-pyplot-text-py]

import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.xlim(40, 160)
plt.ylim(0, 0.03)
plt.grid(True)
plt.show()
image.png

相关文章

  • matplotlib.pyplot.hist绘制直方图

    第一:导入包 第二:造数据 第三:参数解释 plt.hist(x,bins=None,range=None,den...

  • R语言可视化(四):频率直方图绘制

    04.直方图绘制 清除当前环境中的变量 设置工作目录 hist函数绘制频率直方图 ggplot2包绘制直方图 gg...

  • bar

    matlab中函数bar绘制直方图中的应用函数bar(x)可以绘制直方图

  • 绘制直方图

    练习:绘制直方图

  • 50. 彩色直方图源码

    彩色直方图绘制步骤: 读取图片信息 各通道值计数与归一化 设置横纵坐标 绘制蓝绿红直方图 显示所有直方图 彩色直方...

  • 直方图

    一、 绘制直方图 1.1 代码 1.2 效果 二、全局直方图均衡 2.1 代码 1.2 效果 三、直方图匹配 3....

  • OpenCV-Python学习(十一):直方图

    目录: 1.绘制直方图1)一维直方图2)2D直方图 2.直方图均衡化1)全局直方图均衡化2)CLAHE(限制对比度...

  • 认识matplotlib—直方图、饼图、箱线图

    本节主要介绍如何绘制直方图、饼图、箱线图。 直方图 饼图 箱线图

  • 绘制直方图

    《OpenCV轻松入门:面向Python》读书笔记作者:李立宗出版社:电子工业出版社出版时间:2019-05 第1...

  • 计算绘制图像灰度直方图

    OpenCV中分析绘制直方图并没有直接可用的函数,以下是绘制直方图统计的一种实现。paint_histogram....

网友评论

      本文标题:matplotlib.pyplot.hist绘制直方图

      本文链接:https://www.haomeiwen.com/subject/gdjsphtx.html