Matplotlib学习笔记——频次直方图、数据区间划分和分布密度

63 篇文章 6 订阅
39 篇文章 0 订阅

频次直方图、数据区间划分和分布密度

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')

data = np.random.randn(1000)
#最基本的频次直方图命令
plt.hist(data)

这里写图片描述

#调节具体参数
#bins调节横坐标分区个数,alpha参数用来设置透明度
plt.hist(data, bins=30, normed=True, alpha=0.5, histtype='stepfilled',
         color='steelblue', edgecolor='none'

这里写图片描述

#对不同的分布特征的样本进行对比时,将histtype=‘stepfilled’与透明性设置参数alpha搭配使用效果好
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()
counts, bin_edges = np.histogram(data, bins=50)
print(counts)

#输出结果:
[  1   1   2   4   6   9  16  16  28  34  52  42  61  85 135 172 188 231
 295 315 343 386 383 400 401 364 325 319 279 240 195 147 136 100  80  69
  40  28  23  11   9   9   7   3   4   2   1   1   1   1]
二维频次直方图与数据区间划分
plt.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')

这里写图片描述

#如果只想计算各个区间的样本数,可以使用np.histogram2d()
counts, xedges, yedges = np.histogram2d(x, y, bins=30)
plt.hexbin:六边形区间划分
plt.hexbin(x, y, gridsize=30, cmap='Blues')
cb = plt.colorbar(label='count in bin')

这里写图片描述

  • 16
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值