频次直方图、数据区间划分额分布密度——Note_6

1、简单的频次直方图:

plt.hist()文档:https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.hist.html#matplotlib.pyplot.hist

import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn-white')
data = np.random.randn(1000)
plt.hist(data)

在这里插入图片描述
自定义的频次直方图:

plt.hist(data, bins=30, normed=True, alpha=0.5, histtype='stepfilled',
        color='steelblue', edgecolor='none')
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=40)
plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs)

在这里插入图片描述
如果想要简单的计算每段区间的样本数,可以直接用 np.histogram():
https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

>>>counts, bin_edgs = np.histogram(data, bins=5)
>>>print(counts)
[ 23 292 499 171  15]
>>>print(bin_edgs) # 返回bin的边缘
[-3.20401671 -1.85158271 -0.4991487   0.85328531  2.20571931  3.55815332]
二、二维频次直方图与数据划分
1、plt.hist2d: 二维频次直方图

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.hist2d.html#matplotlib.pyplot.hist2d

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('counts in bin')

在这里插入图片描述
与plt.hist() 函数一样,plt.hist2d 也有许多调整图形与区间划分的配置选项。

2、plt.hexbin: 六边形区间划分

二维频次直方图是由与坐标轴正交的方块分割而成的,还有一种常用的方式是用正六边形分割。

plt.hexbin(x, y, gridsize=30, cmap='Blues')
cb = plt.colorbar(label='count in bin')

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值