matplotlib条形图errbar
问题描述:
在官方的例子中,我们可以用条功能是这样的:matplotlib条形图errbar
#!/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)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
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,align='center')
p2 = plt.bar(ind, womenMeans, width, color='y',bottom=menMeans, yerr=menStd,align='center')
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0,81,10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))
plt.show()
我不明白的是,为什么womenStd用作menMeans的yerr, 和menStd被用作女性的手段。
请问你能解释一下吗? 非常感谢。
答
它可能非常可能是一个错字。对于这个例子来说,它看起来并不重要,它可以说明matplotlib的功能。
答
这必须是文档中的拼写错误,可以通过更改每个组中项目的数量来演示。例如,从每个women
变量中删除最后一项,并创建一个新的ind2 = np.arange(N-1)
并重新运行代码。你会得到一个堆栈跟踪,这表明你的直觉是正确的,你应该使用类似的命名变量。显然,给出的例子仅仅是用于说明,但可能应该更改为更清楚。