NumPy使用 Matplotlib 绘制直方图

使用NumPy与Matplotlib绘制直方图
本文介绍如何使用NumPy的numpy.histogram()函数和Matplotlib的plt.hist()函数来绘制数据的频率分布直方图。通过具体示例展示了如何设置bin区间并生成直方图图形。

NumPy - 使用 Matplotlib 绘制直方图

NumPy 有一个numpy.histogram()函数,它是数据的频率分布的图形表示。 水平尺寸相等的矩形对应于类间隔,称为bin,变量height对应于频率。

numpy.histogram()

numpy.histogram()函数将输入数组和bin作为两个参数。 bin数组中的连续元素用作每个bin的边界。

import numpy as np 

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) ]
np.histogram(a,bins =  [0,20,40,60,80,100]) 
hist,bins = np.histogram(a,bins =  [0,20,40,60,80,100])  
print hist 
print bins
Python

输出如下:

[3 4 5 2 1]
[0 20 40 60 80 100]
Python

plt()

Matplotlib 可以将直方图的数字表示转换为图形。 pyplot子模块的plt()函数将包含数据和bin数组的数组作为参数,并转换为直方图。

from matplotlib import pyplot as plt 
import numpy as np  

a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) 
plt.hist(a, bins =  [0,20,40,60,80,100]) 
plt.title("histogram") 
plt.show()
Python

输出如下:

转载于:https://www.cnblogs.com/navysummer/p/9640894.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值