ChristineB ChristineB - 7 days ago 5
Python Question

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

The issue

I have a dot plot I made from imported data. One set of data shows a time series of means across various members of a dataset, and the other shows the standard deviation (SD) of these members at each time step, before the means were taken. My advisor wants me to add "whiskers" that show +/- 1 SD to the points representing the means. I'll provide a simplified, working example below.

The code (example)

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 was multiplied by 5 so whiskers, when added, could actually be visible. The result should look like this.
enter image description here

The Question

How can I add whiskers to the triangles that represent +/- 1 corresponding SD (the values of the circles below)? Given my actual data isn't showing a range along the y-direction, I don't think a box plot would work best for this...

Answer

You could add error bars with:

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

If yerr is a 1D array of size time, the error bars are drawn at +/- yerr relative to your data (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.

With your code, that results in:

enter image description here

For more info, see these examples: http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html