 # error bar and arrays (tuple way to solve a problem)

Hello to all
I've discovered Numpy and Matplotlib two weeks ago and I'm trying to free myself to the slavery of Matlab (TM) and I'm converting all my small sotware about chemometrics in python (and I will be happy to release them soon to the community)
I have to make a plot with errorbars (I take two vectors from previous calculation). I thuoght it was an esay task... but.. let's see what happened

first try withous error bars

#!/usr/bin/env python
# a stacked bar plot with errorbars
from pylab import *
#womenMeans = (25, 32, 34, 20, 25)
#womenStd = (2, 3, 4, 1, 2)
#womenStd = (3.1, 5.1, 2.3, 3.5, 3.4)
############### it works
##N=5
##menMeans = array([, , , , ])
##ind = arange(N) # the x locations for the groups
##width = 0.35 # the width of the bars: can also be len(x) sequence
##p1 = bar(ind, womenMeans, width, color='r', )

and it worked !!!

simple plot with error (but this time with sequences..)

################ it works
##womenStd = (3.1, 5.1, 2.3, 3.5, 3.4)
##N=5
##menMeans = (20, 35, 30, 35, 27)
##ind = arange(N) # the x locations for the groups
##width = 0.35 # the width of the bars: can also be len(x) sequence
##p1 = bar(ind, womenMeans, width, color='r', yerr=womenStd)

it works... so I thought.. I will add error bars and be ok ...BUT....

############## it doesnt'works
##womenStd = array([[3.1], [5.1], [2.3], [3.5], [3.4]])
##N=5
##womenMeans = array([, , , , ])
##ind = arange(N) # the x locations for the groups
##width = 0.35 # the width of the bars: can also be len(x) sequence
##p1 = bar(ind, womenMeans, width, color='r', yerr=womenStd)
##Traceback (most recent call last):
## File "C:\Python25\Lib\site-packages\matplotlib\examples\bar_stacked.py", line 34, in -toplevel-
## p1 = bar(ind, menMeans, width, color='r', yerr=womenStd)
## File "C:\Python24\Lib\site-packages\matplotlib\pylab.py", line 1651, in bar
## ret = gca().bar(*args, **kwargs)
## File "C:\Python24\Lib\site-packages\matplotlib\axes.py", line 2572, in bar
## fmt=None, ecolor=ecolor, capsize=capsize)
## File "C:\Python24\Lib\site-packages\matplotlib\axes.py", line 2904, in errorbar
## barlines.extend( self.vlines(x, y, upper, label='_nolegend_' ) )
## File "C:\Python24\Lib\site-packages\matplotlib\axes.py", line 2032, in vlines
## color=color, linestyle=linestyle, marker=marker,
## File "C:\Python24\Lib\site-packages\matplotlib\lines.py", line 206, in __init__
## self.set_data(xdata, ydata)
## File "C:\Python24\Lib\site-packages\matplotlib\lines.py", line 269, in set_data
## raise RuntimeError('xdata and ydata must be the same length')
##RuntimeError: xdata and ydata must be the same length

···

#############

What's the matter ???
After spending an hour searching what a tuple, and a sequence is… I've solved the problem in this way…

womenStd = array([[3.1], [5.1], [2.3], [3.5], [3.4]])
womenStdFlatten=womenStd.flatten()
womenStdTuple=tuple(womenStdFlatten)
N=5
womenMeans = array([, , , , ])
womenFlatten=womenMeans.flatten()
womenTuple=tuple(womenFlatten)
ind = arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
p1 = bar(ind, womenTuple, width, color='r', yerr=womenStdTuple)

ylabel('Scores')
title('Scores by group and gender')
#xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') )
#yticks(arange(0,81,10))
#legend( (p1, p2), ('Men', 'Women') )

show()

Does anyone know if this is the only solution to get it work ???
I hope to have helped someone with the same problem like me and also curious to know if there is any other solution

Used python 2.4 scypy 1.0 , and the last version of numpy and matplotlib avaible for them

Cheers
Giorgio

Giorgio Luciano wrote:
[...]

############## it doesnt'works
##womenStd = array([[3.1], [5.1], [2.3], [3.5], [3.4]])
##N=5
##womenMeans = array([, , , , ])
##ind = arange(N) # the x locations for the groups
##width = 0.35 # the width of the bars: can also be len(x) sequence
##p1 = bar(ind, womenMeans, width, color='r', yerr=womenStd)

The problem is that you are creating 2-D arrays where you need simple lists or 1-D arrays. See attached script.

Eric

bartest.py (370 Bytes)

When I look at http://matplotlib.sourceforge.net/tutorial.html with Safari, I see a lot of broken images. Any ideas?