Python- 如何将 "whiskers" 添加到点图中?

Python- How can I add "whiskers" to a dot plot?

问题

我有一个用导入数据制作的点图。一组数据显示数据集各个成员的时间序列平均值,另一组数据显示这些成员在采用平均值之前每个时间步长的标准差 (SD)。我的顾问要我添加 "whiskers" 以显示 +/- 1 SD 到代表均值的点。我将在下面提供一个简化的工作示例。

代码(示例)

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

#Generate some data
x = np.empty((7,9))
for i in range(7):
    x[i,:] = np.arange(1,10,(1+(i*.02)))
print x

#Find standard devs down each column
std = np.std(x,axis=0)*5
print std

#Get the mean of x data down each column
xmean = np.mean(x,axis=0)
print xmean

#Plot xmean data & x's stan. devs
legendlabels = ['Mean of members','S.D. of members']
time = np.arange(0,9)
fig, ax = plt.subplots(figsize=(11,6))
data1   = ax.scatter(time,xmean,s=70,color='k',marker='^')
data2   = ax.scatter(time,std,  s=70,color='k')
ax.legend([data1,data2],legendlabels,loc=2)
ax.grid()
plt.show()

SD 乘以 5,因此添加后的胡须实际上是可见的。结果应该是这样的。

问题

如何向表示 +/- 1 对应 SD(下面圆圈的值)的三角形添加胡须?鉴于我的实际数据未显示沿 y 方向的范围,我认为箱形图最适合此...

您可以添加错误栏:

ax.errorbar(time, xmean, yerr=std)

如果 yerr 是大小为 time 的一维数组,误差线绘制在相对于您的数据的 +/- yerr (xmean) (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.errorbar):

If a scalar number, len(N) array-like object, or an Nx1 array-like object, errorbars are drawn at +/-value relative to the data.

使用您的代码,结果为:

有关详细信息,请参阅以下示例:http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html