matplotlib高级堆叠条

时间:2013-05-31 02:14:16

标签: matplotlib

matplotlib情节栏

它可以像http://matplotlib.org/examples/api/barchart_demo.html

一样规则

我们将其定义为[M,F]

它可以像http://matplotlib.org/examples/pylab_examples/bar_stacked.html

一样堆叠

我们将其定义为[M + F]

现在如何绘制[M,F +其他]

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望堆叠两个以上元素的堆栈图?如果是的话,那就像你发布的例子那样直截了当:

#!/usr/bin/env python
# a stacked bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt


N = 5
menMeans   = [20, 35, 30, 35, 27]
womenMeans = [25, 32, 34, 20, 25]
otherMeans = [5, 2, 4, 8, 5]
menStd     = [2, 3, 4, 1, 2]
womenStd   = [3, 5, 2, 3, 3]
otherStd   = [1, 1, 1, 1, 1]
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence

p1 = plt.bar(ind, menMeans,   width, color='r', yerr=womenStd)
p2 = plt.bar(ind, womenMeans, width, color='y',
             bottom=menMeans, yerr=menStd)
p3 = plt.bar(ind, otherMeans, width, color='b',
             bottom=[menMeans[j] + womenMeans[j] for j in range(len(menMeans)) ], 
             yerr=otherStd)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') )
plt.yticks(np.arange(0,81,10))
plt.legend( (p1[0], p2[0], p3[0]), ('Men', 'Women', 'Other') )

plt.show()