Matplotlib——直方图_hist()函数_histogram()函数_二维数据

一个简单的直方图可以直观地展示数据的分布,包括数值分布的区间、密度和形状

在实际的工作过程中,我们可能需要对数据进行数学建模统计分析,这些数据处理技术往往基于数据符合的某些假设,而直方图是检查数据最好的选择之一。

下面通过 NumPy 模块提供的随机数据生成函数,产生符合正态分布的随机数据,并以它为样例绘制直方图。

import numpy as np
import matplotlib.pyplot as plt

randn_data = np.random.randn(1000)

plt.hist(randn_data)

plt.show()

请添加图片描述
这里用到了hist()函数。

plt.hist(x, bins=10, range=None, normed=False,
		weights=None, cumulative=False, bottom=None,
		histtype='bar', align='mid', orientation='vertical',
		rwidth=None, log=False, cokor=None,
		label=None, satacked=False)

“●”的打法:在英文输入法下长按alt然后输入 41457 41457 41457松开alt就有了

x :指定要绘制直方图的数据。
bins :指定直方图条形的个数。
range :指定直方图数据的上下边界,默认包含绘图数据的最大值和最小值。
density :是否将直方图的频数转换成频率。
weights :为每一个数据点设置权重。
cumulative :是否需要计算累计频数或频率。
bottom :为直方图的每个条形添加基准线,默认为0。
histtype :指定直方图的类型,默认为 bar,此外还有’barstacked’、‘step’、‘stepfilled’。
align :设置条形边界值的对齐方式,默认为mid,此外还有 ‘left’ 和 ‘right’。
orientation :设置直方图的放方向,默认为垂直方向。
rwidth :设置直方图条形宽度的百分比。
log :是否需要对绘图数据进行 log 变换。
color :设置直方图的填充色。
label :设置直方图的标签,可通过 legend 展示其图例。
stacked :当有多个数据时,是否需要将直方图呈堆叠放,默认水平放。

下面尝试一些案例

plt.hist(randn_data,bins=30,
		density=True,histtype='step',
		color='steelblue')

请添加图片描述
如果要比较多个数据的分布,可以使用选项histtype=‘stepfilled’,并设置一定的透明度。

import numpy as np
import matplotlib.pyplot as plt

randn_data = np.random.randn(1000)

_ = plt.hist(randn_data, bins=30, density=True, histtype='step', color='steelblue')

x1 = np.random.normal(0, 0.4, 1000)
x2 = np.random.normal(-3, 1, 1000)
x3 = np.random.normal(2, 2, 1000)

kwargs = dict(histtype='stepfilled', alpha=0.5, density=True, bins=50)

_ = plt.hist(x1, **kwargs)
#"**"意思为设置默认参数
_ = plt.hist(x2, **kwargs)
_ = plt.hist(x3, **kwargs)

plt.show()

请添加图片描述
有时我们不仅想通过直方图直观地看到数据,而且想获取不同条形个数区间的频数/频率。NumPy提供的histogram()函数可以满足这个需求。

下面从randn_data中提取设置10个条形时的各自的频数。

import numpy as np
import matplotlib.pyplot as plt

randn_data = np.random.randn(1000)

counts, bin_edges = np.histogram(randn_data, bins=10)

print(counts)

输出:

[  9  31  73 153 235 205 166  87  33   8]

Process finished with exit code 0

设置density=True可以获取相应的频率。

density,bin_edges=np.histogram(randn_data,bins=10,density=True)

print(density)

输出:

[0.01329965 0.04137668 0.12265231 0.27190391 0.38421204 0.32214702
 0.21870532 0.07832015 0.0192106  0.00591095]

Process finished with exit code 0

如果使用的是二维数据,Matplotlib同样提供了hist2d()函数用于查看数据的分布。一维数据中直方图将数据切分到不同的区间中,而二维直方图在两个维度进行切分,因此会得到一个一个的小矩形。

下面演示二维数据绘图

import numpy as np
import matplotlib.pyplot as plt

mean = [0, 0]  # 均值
cov = [[1, 1], [1, 2]]  # 协方差矩阵
x, y = np.random.multivariate_normal(mean, cov, 10000).T

# 绘制图形
plt.hist2d(x, y, bins=30, cmap='Blues')
cb = plt.colorbar()
cb.set_label(u'计数')

plt.show()

请添加图片描述

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值