python绘制条形图方法代码详解


Posted in Python onDecember 19, 2017

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()

python绘制条形图方法代码详解

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()

python绘制条形图方法代码详解

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()

python绘制条形图方法代码详解

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()

python绘制条形图方法代码详解

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()

python绘制条形图方法代码详解

6大家根据自己的需要自己来绘制自己的条形图

下面回答网友提问,如何画在条形图上垂直显示数据:

下面这个函数是用来垂直显示的,其中设置角度就可以以任意方式来显示。

def autolabel(rects,Num=1.12,rotation1=90,NN=1):
    for rect in rects:
      height = rect.get_height()
      plt.text(rect.get_x()-0.04+rect.get_width()/2., Num*height, '%s' % float(height*NN),rotation=rotation1)

调用方式如下

rects1 =plt.bar(left = (0.05),height = (Pe_FH),color=('b'),label=('FHMM'),width = 0.1,align="center",yerr=0.000001);
autolabel(rects1,1.09);

下面是效果图

python绘制条形图方法代码详解

总结

以上就是本文关于python绘制条形图方法代码详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参考本站:

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
Python中使用dom模块生成XML文件示例
Apr 05 Python
python使用线程封装的一个简单定时器类实例
May 16 Python
python修改操作系统时间的方法
May 18 Python
如何在Python函数执行前后增加额外的行为
Oct 20 Python
一个月入门Python爬虫学习,轻松爬取大规模数据
Jan 03 Python
python 通过麦克风录音 生成wav文件的方法
Jan 09 Python
python向字符串中添加元素的实例方法
Jun 28 Python
python使用百度文字识别功能方法详解
Jul 23 Python
在PyTorch中Tensor的查找和筛选例子
Aug 18 Python
python爬虫之爬取百度音乐的实现方法
Aug 24 Python
django drf框架自带的路由及最简化的视图
Sep 10 Python
Python timeit模块原理及使用方法
Oct 10 Python
Python实现两款计算器功能示例
Dec 19 #Python
Python构建网页爬虫原理分析
Dec 19 #Python
Python2.X/Python3.X中urllib库区别讲解
Dec 19 #Python
Python实现采用进度条实时显示处理进度的方法
Dec 19 #Python
Python实现矩阵加法和乘法的方法分析
Dec 19 #Python
分析python切片原理和方法
Dec 19 #Python
python实现redis三种cas事务操作
Dec 19 #Python
You might like
E路文章系统PHP
2006/12/11 PHP
cmd下运行php脚本
2008/11/25 PHP
php实现判断访问来路是否为搜索引擎机器人的方法
2015/04/15 PHP
php源码分析之DZX1.5字符串截断函数cutstr用法
2015/06/17 PHP
PHP编程基本语法快速入门手册
2016/01/07 PHP
PHP中的日期时间处理利器实例(Carbon)
2017/06/09 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
PHP设计模式(五)适配器模式Adapter实例详解【结构型】
2020/05/02 PHP
PHP unset函数原理及使用方法解析
2020/08/14 PHP
Javascript 面向对象 对象(Object)
2010/05/13 Javascript
扩展javascript的Date方法实现代码(prototype)
2010/11/20 Javascript
javascript中类的定义及其方式(《javascript高级程序设计》学习笔记)
2011/07/04 Javascript
jQuery插件实现大图全屏图片相册
2015/03/14 Javascript
jQuery EasyUI Dialog拖不下来如何解决
2015/09/28 Javascript
深入理解JQuery中的事件与动画
2016/05/18 Javascript
JS 通过系统时间限定动态添加 select option的实例代码
2016/06/09 Javascript
VUE使用vuex解决模块间传值问题的方法
2017/06/01 Javascript
angular select 默认值设置方法
2017/06/23 Javascript
JavaScript函数、闭包、原型、面向对象学习笔记
2018/09/06 Javascript
react实现同页面三级跳转路由布局
2019/09/26 Javascript
解决vue-router 切换tab标签关闭时缓存问题
2020/07/22 Javascript
Vue中登录验证成功后保存token,并每次请求携带并验证token操作
2020/09/08 Javascript
[02:44]DOTA2英雄基础教程 魅惑魔女
2014/01/07 DOTA
关于你不想知道的所有Python3 unicode特性
2014/11/28 Python
django框架如何集成celery进行开发
2017/05/24 Python
windows 下python+numpy安装实用教程
2017/12/23 Python
TensorFlow实现MLP多层感知机模型
2018/03/09 Python
Python 多线程其他属性以及继承Thread类详解
2019/08/28 Python
scrapy-splash简单使用详解
2021/02/21 Python
在线吉他课程,学习如何弹吉他:Fender Play
2019/02/28 全球购物
售后服务科岗位职责范文
2013/11/13 职场文书
中班开学寄语
2014/04/04 职场文书
幼儿园大班家长评语
2014/04/17 职场文书
实习协议书
2015/01/27 职场文书
2015年企业员工工作总结范文
2015/05/21 职场文书
MySQL 自动填充 create_time 和 update_time
2022/05/20 MySQL