python绘制条形图方法代码详解
作者:-dragon- 时间:2022-11-30 00:07:19
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绘制铅球的运行轨迹代码分享
用Pygal绘制直方图代码示例
Python中pygal绘制雷达图代码分享
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
来源:http://blog.csdn.net/yywan1314520/article/details/50818471
标签:python,条形图
0
投稿
猜你喜欢
使用python装饰器验证配置文件示例
2022-05-27 03:10:11
一个jquery日期选取插件源码
2009-12-23 19:15:00
Python读取一个目录下所有目录和文件的方法
2023-05-30 23:04:21
Django如何重置migration的几种情景
2021-11-17 14:35:34
ie7空格的间距要比ie6/firefox/opera的都要大
2008-05-24 16:54:00
css中使用CLASS来设计网页布局
2007-10-30 13:01:00
Python之Pygame的Draw绘图
2022-11-29 18:51:18
让我们走进ASP.NET世界
2007-08-24 08:52:00
asp的日期转换星座函数
2010-06-09 21:05:00
如何自动备份Oracle数据库
2008-06-13 16:54:00
DreamweaverMX 2004打造细线表格
2008-10-01 09:39:00
ORACLE 自动提交问题
2023-07-24 10:43:13
django框架model orM使用字典作为参数,保存数据的方法分析
2021-03-11 00:18:30
ASP 自动采集实现代码
2011-03-07 11:17:00
创建Shapefile文件并写入数据的例子
2023-06-21 23:27:12
python数据可视化Seaborn画热力图
2022-01-17 22:55:05
python实现Pyecharts实现动态地图(Map、Geo)
2021-01-21 11:33:58
Python日志:自定义输出字段 json格式输出方式
2022-08-20 01:27:19
详解python如何调用C/C++底层库与互相传值
2022-02-25 07:18:00
精细分析 SQL server服务器的内存配置
2009-01-19 13:56:00