首页 > 其他 > 详细

matplotlib 绘制柱状图的几个例子

时间:2015-11-27 17:11:43      阅读:980      评论:0      收藏:0      [点我收藏+]

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 = (524, 572, 630, 680, 729, 777, 820)
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., (2010, 2015, 2020, 2025, 2030, 2035, 2040))
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()

技术分享

matplotlib 绘制柱状图的几个例子

原文:http://www.cnblogs.com/yxzfscg/p/5000867.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!