python 直方图的绘制方法全解
2016-03-07 13:22
786 查看
1.首先要绘制一个简单的直方图
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') plt.show()
1.1 上面中rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align=”center”,yerr=0.000001)这句代码是最重要的,其中left表示直方图的开始的位置(也就是最左边的地方),height是指直方图的高度,当直方图太粗时,可以通过width来定义直方图的宽度,注意多个直方图要用元组,yerr这个参数是防止直方图触顶。
2. 增加直方图脚注
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') plt.xticks((0.2,1),('frst','second')) plt.show()
3.直方图上显示具体的数字(自动编号)
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height)) autolabel(rects) plt.xticks((0.2,1),('frst','second')) plt.show()
4.改变直方图颜色
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects =plt.bar(left = (0.2,1),height = (1,0.5),color=('r','g'),width = 0.2,align="center",yerr=0.000001) plt.title('Pe') def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height)) autolabel(rects) plt.xticks((0.2,1),('frst','second')) plt.show()
5.添加图注
import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from matplotlib import rcParams fig1 = plt.figure(2) rects1 =plt.bar(left = (0.2),height = (0.5),color=('g'),label=(('no1')),width = 0.2,align="center",yerr=0.000001) rects2 =plt.bar(left = (1),height = (1),color=('r'),label=(('no2')),width = 0.2,align="center",yerr=0.000001) plt.legend() plt.xticks((0.2,1),('frst','second')) plt.title('Pe') def autolabel(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, '%s' % float(height)) autolabel(rects1) autolabel(rects2) plt.show()
6大家根据自己的需要自己来绘制自己的直方图
相关文章推荐
- Python ORM SQLAlchemy 的中文乱码问题解决
- 用键盘控制鼠标移动的Python脚本
- 用 python 实现一个多线程网页下载器
- 简明 Python 编程规范v2
- Python 字符串方法详解
- Python温故
- Python的zip函数
- python学习随笔--web框架
- 源码:自己用Python写的iOS项目自动打包脚本
- 输入 pydoc print 提示 SyntaxError: invalid syntax
- Programming Language :: Python :: Understanding Twisted Deferred
- 关于opencv3.1编译的时候找不到python的lib的解决方法
- python学习之模块:xlsxwriter
- python urlopen SSL: CERTIFICATE_VERIFY_FAILED
- python中try except处理程序异常的三种常用方法
- Python ROM SQLAlchemy Document
- Python如何进行中文注释
- 机器学习算法 Python & R速查表
- 【python】编程语言入门经典100例--22
- Python基础(Django)