matplotlib 绘制柱状图的几个例子
2024-09-28 13:41:39
1 error bar
#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2) ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd) womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd) # add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) ax.legend((rects1[0], rects2[0]), ('Men', 'Women')) def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom') autolabel(rects1)
autolabel(rects2) plt.show()
2 绘制两个量,一个在上,一个在下.关键点在于
p2 = plt.bar(ind, OECD, width, color = 'b', bottom = NonOECD) 这个bottom参数
import numpy as np
import matplotlib.pyplot as plt N = 7
OECD = (242, 244, 255, 263, 269, 276, 285)
NonOECD = (282, 328, 375, 417, 460, 501, 535)
Sum = ('', '', '', '', '', '', '')
ind = np.arange(N)
width = 0.5 p1 = plt.bar(ind, NonOECD, width, color = 'r')
p2 = plt.bar(ind, OECD, width, color = 'b', bottom = NonOECD) plt.ylabel('Quadrillion Btu')
plt.title('World Total Energy Consumption 2010 - 2040')
plt.xticks(ind+width/2., ('', '', '', '', '', '', ''))
plt.yticks(np.arange(0, 1001, 200))
plt.legend((p1[0], p2[0]), ('Non - OECD', 'OECD'), loc = 2, frameon = 'false')
plt.tick_params(top = 'off', bottom = 'off', right = 'off')
plt.grid(axis = 'y', linestyle = '-') plt.show()
3 默认的x轴刻度是[1,...],而且一般会缩略,可是我们希望x轴刻度是x轴数据本身的范围。
如果这样
axes.bar(bin_index, num_list, bar_width, color='r')
axes.set_xticks(xticks)
是得不到我们想要的,因为在缩略后,刻度可能只有4个,而set_xticks时,就变成只有这四个刻度上有值,而且这些值不是想象中得自动缩略得到,而是只有xticks这个列表中前四个,这样就与我们的要求不符合
最新文章
- UED双飞翼布局
- 行为型模式之Template Method模式
- wep.py输出hello world
- WPF使用cefsharp
- C++ 中常见预定义宏的使用
- 对MMU段式转换的理解
- canvas实现画板功能(渐变、动画、阴影...)
- 巧用Dictionary<;TKey,TValue>;,完成客户需求
- Django-4 视图层
- [Kubernetes]yaml文件详解
- vm中的nat模式不能连接到主机,以及不能上网
- 51-nod(1443)(最短路)
- PDF如何去除背景,PDF去除背景颜色
- Python开发——基础
- FZU软工第五次作业-词组频率分析
- H. GSS and Simple Math Problem 高精度乘法模板
- debian下配置keepalived ha
- BZOJ3157/BZOJ3516 国王奇遇记(矩阵快速幂/数学)
- 【BZOJ4452】[Cerc2015]Export Estimate 并查集
- 关于防SQL注入敏感词过滤问题
热门文章
- iOS终端查看.a文件是否能在模拟器上运行
- POJ 2127
- iOS多线程的初步研究(六)-- NSOperation
- x64 结构体系下的内存寻址
- sshpass
- SQL server 为多个表添加新的列
- 如何开启Centos6.4系统的SSH服务
- How a woman&#39;s glance can tell a man if she&#39;s interested
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-003-四种方式获取DataSource
- 在Ogre中加载自己的资源包