4.matplotlib绘制直方图

?

?

# coding=utf-8
from matplotlib import pyplot as plt
from matplotlib import font_manager

a=[131,  98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115,  99, 136, 126, 134,  95, 138, 117, 111,78, 132, 124, 113, 150, 110, 117,  86,  95, 144, 105, 126, 130,126, 130, 126, 116, 123, 106, 112, 138, 123,  86, 101,  99, 136,123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127,105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114,105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134,156, 106, 117, 127, 144, 139, 139, 119, 140,  83, 110, 102,123,107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133,112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135,115, 146, 137, 116, 103, 144,  83, 123, 111, 110, 111, 100, 154,136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141,120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126,114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137,  92,121, 112, 146,  97, 137, 105,  98, 117, 112,  81,  97, 139, 113,134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110,105, 129, 137, 112, 120, 113, 133, 112,  83,  94, 146, 133, 101,131, 116, 111,  84, 137, 115, 122, 106, 144, 109, 123, 116, 111,111, 133, 150]

#计算组数
d = 3  #组距
num_bins = (max(a)-min(a))//d
print(max(a),min(a),max(a)-min(a))
print(num_bins)

#设置图形的大小
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a,num_bins,normed=True)

#设置x轴的刻度
plt.xticks(range(min(a),max(a)+d,d))

plt.grid()

plt.show()

显示效果

?

原文地址:https://www.cnblogs.com/cmi-sh-love/p/4matplotlib-hui-zhi-zhi-fang-tu.html

时间: 2024-08-07 12:05:15

4.matplotlib绘制直方图的相关文章

numpy和matplotlib绘制直方图

使用 Matplotlib Matplotlib 中有直方图绘制函数:matplotlib.pyplot.hist()它可以直接统计并绘制直方图.你应该使用函数 calcHist() 或 np.histogram()统计直方图. 1 使用pyplot.hist() 显示灰度图像直方图,代码如下: import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('image/lufei.jpeg

matplotlib:直方图的绘制

1.matplotlib绘图三部曲 (1)创建画布 (2)绘图——包含绘图与图形修饰 (3)绘图展示 2.直方图的绘制 (1)导入模块 import matplotlib.pyplot as pltimport numpy as np 将模块导入到脚本中,并更改名字为plt (2)创建画布 plt.figure(figsize=(8,5), dpi=120) 使用figure()方法创建画布,figsize=(x,y)参数用于设置画布大小,单位为英寸,dpi参数用于设置图片像素 matplotl

绘制直方图

# coding = utf - 8 # 直方图用于那些没有统计过的数据 from matplotlib import pyplot as plt a = [131,98,125,131,124,139,131,117,128,108,135,138,131,102, 97] # 计算组数 d = 3 # 组距 num_bins = (max(a) - min(a)) // d print(max(a), min(a), max(a) - min(a)) print(num_bins) # 设置

matplotlib:使用matplotlib绘制图表

matplotlib下载及API手册地址:http://sourceforge.net/projects/matplotlib/files/matplotlib/ 数学库numpy下载及API手册地址:http://www.scipy.org/Download 几个绘图的例子[来自API手册] 1.最简单的图: 代码: #!/usr/bin/env python import matplotlib.pyplot as plt plt.plot([10, 20, 30]) plt.xlabel('

广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小

迭代公式的指数,使用的1+5j,这是个复数,所以是广义mandelbrot集,大家可以自行修改指数,得到其他图形.各种库安装不全的,自行想办法,可以在这个网站找到几乎所有的python库 http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib #encoding=utf-8 import numpy as np import pylab as pl import time from matplotlib import cm from math

用Python绘制直方图

计算频数: 给定一个序列t: hist = {} for x in t: hist[x] = hist.get(x,0)+1 得到的结果是一个将值映射到其频数的字典.将其除以n即可把频数转换成频率,这称为归一化: n = float(len(t)) pmf = {} for x, freq in hist.items(): pmf[x] = freq/n 绘制直方图: Vals, freqs = hist.Render() rectangles = pyplot.bar(vals, freqs)

在图片上画矩形并高亮显示矩形区域、统计矩形区域中像素情况并绘制直方图

<学习OpenCV>中文版第4章第3题 提纲 题目要求 程序代码 结果图片 题目要求: ①允许用户在图片上选择一个矩形区域,当鼠标放开,高亮显示矩形区域 ②在另一个独立窗口中,使用绘图函数绘制一个图表,分别用蓝.绿和红色表示选中区域中各种颜色的像素在指定数值范围内的数量. 程序代码: 1 #include "stdafx.h" 2 #include <cv.h> 3 #include <highgui.h> 4 using namespace std

OpenCV2马拉松第8圈——绘制直方图

收入囊中 灰度直方图 彩色直方图 葵花宝典 直方图的理论还是非常丰富的,应用也很多,诸如: 直方图均衡化 直方图匹配(meanshift,camshift) 在这里,我先介绍基础,如何绘制图像的直方图. 拿灰度图像来说,直方图就是不同的灰度对应的个数,横轴(x)就是[0,256), 纵轴(y)就是对应的个数 如下图,分别是灰度直方图和彩色直方图 初识API C++: void calcHist(const Mat* images, int nimages, const int* channels

Python + Matplotlib 绘制 Penrose 铺砌

效果是不是很漂亮呢? 代码如下: #----------------------------------------- # Python + Matplotlib 绘制 Penrose 铺砌 # by Zhao Liang [email protected] #----------------------------------------- import matplotlib.pyplot as plt import numpy as np from matplotlib.path impor