-1

matplotlib 绘图条

它可以是常规的,例如http://matplotlib.org/examples/api/barchart_demo.html

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

它可以像http://matplotlib.org/examples/pylab_examples/bar_stacked.html一样堆叠

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

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

4

1 回答 1

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()
于 2013-05-31T09:24:58.643 回答