python画条形图-python绘制条形图方法代码详解
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大家根据自己的需要自己来绘制自己的条形图
下面回答网友提问,如何画在条形图上垂直显示数据:
下面这个函数是用来垂直显示的,其中设置角度就可以以任意方式来显示。
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怎么画条形图-python绘制条形图方法代码详解
1.首先要绘制一个简单的条形图 import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from ...
- python绘制条形图用什么函数_python绘制条形图方法代码详解
1.首先要绘制一个简单的条形图 import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from ...
- 如何在python制作计算器_Python简易计算器制作方法代码详解
主要用到的工具是Python中的Tkinter库 比较简单 直接上图形界面和代码 引用Tkinter库 from tkinter import * 建立主窗口对象 window=Tk() #设置窗口对 ...
- python装饰器函数-Python函数装饰器常见使用方法实例详解
本文实例讲述了Python函数装饰器常见使用方法.分享给大家供大家参考,具体如下: 一.装饰器 首先,我们要了解到什么是开放封闭式原则? 软件一旦上线后,对修改源代码是封闭的,对功能的扩张是开放的,所 ...
- python 制作抽奖_python制作抽奖程序代码详解
实现制作抽奖程序,需要认知到我们可以看到一般抽奖程序界面上是有很多按钮的,比如中奖区域,按键开始区域等等,所以我们先要设置界面,然后把这些按钮添加到界面中去,想必这对于学过tkinter的同学应该不难 ...
- Python生成器next方法和send方法区别详解
yield的语法规则是: 在yield这里暂停函数执行,并返回yield后面表达式的值(默认为None),直到被next()再次调用时,从上次暂停的yield代码处继续往下执行.当没有可继续next( ...
- Python文本特征提取 DictVectorizer CountVectorizer TfidfVectorizer 附代码详解
文章目录 DictVectorizer 对使用字典储存的数据进行特征提取与向量化 CountVectorizer / TfidfVectorizer 处理无特殊数据结构存储的数据 词袋模型(Bag o ...
- Python的getter和setter的方法使用详解
本文主要内容: 解释setter和getter的使用方法 解释@property装饰器的妙用 在python中,setter和getter方法并不像其它编程语言中的那样.基本上,在面向对象编程语言中, ...
- python datetime库_python datetime库使用代码详解
python 如何将字符串转化为datetime.date心总是在最痛时,复苏:爱总是在最深时,落下帷幕. 比较省事的办法是用time模块的strptime方法来解析日期字符串成为时间对象,然后再把年 ...
最新文章
- java 观察者模式示例_Java中的观察者设计模式-示例教程
- 路径.git下的文件
- 安装eclipse及android,Eclipse Android 安装
- java持久层用文件_JAVA中用三种方法将字符串持久化到文件中
- 打开文件对话框在xp和win7上的实现文件任意多选
- SpringAOP配置与使用(示例)
- 这40个Python可视化图表案例,强烈建议收藏!
- 拼多多开放平台订单信息查询接口【pdd.order.basic.list.get订单基础信息列表查询接口(根据成交时间)】代码对接教程
- 拼多多API详情接口调用示例
- Amazing!在线浏览 Stata Journal 单篇论文 (2001-2019)
- Docker 容器安装监控软件 cAdvisor
- 使用微PE工具箱安装Win10系统步骤
- html中如何写div中div的位置,position设置div的位置
- 拍照怎么测量尺寸?这些方法可以了解一下
- UVa 11400 Lighting System Design
- HDOJ(HDU) 1570 A C
- Job And Schedule (V8R6C4)
- 如何封禁IP和IP段 看完这篇我会了
- layui框架的使用
- Windows在虚拟机安装苹果MacOS系统