Here's a plot I made using matplotlib
. It uses the bar
and scatter
methods from pylab
.
I have 3 issues:
How to make the error bars fatter? No API in bar
for this that I can see.
How to specify the axes properly?
How to stop the x-axis labels from showing?
The first is most important, as I have no idea. I guess one more thing would be, how to display the image here in SO? I've seen it done but don't know how.
Here is the code:
import numpy as np
from pylab import *
data1 = np.linspace(12,22,7)
data2 = np.random.normal(loc=15,scale=5,size开发者_如何学Python=10)
data3 = [11,12,18,19,20,26,27]
data = [data1,np.abs(data2),data3]
# n = number of groups
def layout(n,r=7):
s = r**2 # r = radius of each data point
#layout from 1 to 100
margin = 5
spacer = 10
group_width = (100 - 2*margin - (n-1)*spacer)*1.0/n
dot_width = r
bar_width = group_width - dot_width
current = margin
rL = list()
for i in range(n):
rL.append(current) # x for point
rL.append(current + 3) # x for bar
current += group_width + spacer
return s, bar_width, rL
s, w, xlocs = layout(len(data))
for group in data:
x = xlocs.pop(0)
for e in group:
scatter(x,e,s=s,color='k')
m = np.mean(group)
e = np.std(group)
x = xlocs.pop(0)
o = bar(x,m,width=w,color='0.6',
yerr=e, ecolor='k')
show()
alt text http://img210.imageshack.us/img210/8503/screenshot20100206at703.png
The error bars are drawn with the errorbar method from within the bar method. It accepts an elinewidth argument but it doesn't look like you can pass it through the bar method call. I would just draw them manually.
o, = bar(x,m,width=w,color='0.6', yerr=None) # note the comma after the o
eBarX = o.get_x()+o.get_width()/2.0
eBarY = o.get_height()
errorbar(eBarX,eBarY,e,capsize=7,elinewidth=6,ecolor='k')
To turn the XAxis off use this before you call show:
axes().xaxis.set_visible(False)
These changes make your plot look like this: alt text http://img690.imageshack.us/img690/5141/testfs.png
Alternatively, to get fat error bars, you can pass "elinewidth" through the "bar"-method as follows:
o = bar(x,m,width=w,color='0.6', error_kw={"elinewidth":5}, yerr=e)
精彩评论