NumPy教程:使用 Matplotlib 绘制直方图

2018-09-2710:51:11后端程序开发Comments3,511 views字数 628阅读模式

NumPy 有一个numpy.histogram()函数,它是数据的频率分布的图形表示。 水平尺寸相等的矩形对应于类间隔,称为bin,变量height对应于频率。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5901.html

numpy.histogram()

numpy.histogram()函数将输入数组和bin作为两个参数。 bin数组中的连续元素用作每个bin的边界。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5901.html

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

输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5901.html

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

plt()

Matplotlib 可以将直方图的数字表示转换为图形。 pyplot子模块的plt()函数将包含数据和bin数组的数组作为参数,并转换为直方图。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5901.html

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.cainiaoxueyuan.com/bc/5901.html

NumPy教程:使用 Matplotlib 绘制直方图文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/5901.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/5901.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定