matplotlib: TypeError: a float is required

A

Andi Clemens

Hi,

everytime I try to plot a bar with matplotlib I get the following error
message:
Traceback (most recent call last):
File "bar_stacked.py", line 13, in ?
p1 = bar(ind, menMeans, width, color='r', yerr=womenStd)
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line
1641, in bar
ret = gca().bar(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line
2485, in bar
xerr = asarray([xerr]*nbars, Float)
File "/usr/lib/python2.4/site-packages/Numeric/Numeric.py", line 134,
in asarray
return multiarray.array(a, typecode, copy=0, savespace=savespace)
TypeError: a float is required


So I guess it has something to do with Numeric.py, but I can't figure
out what the problem is. I tried to google but found nothing...

Anyone has the same problem? I can plot all kinds of graphics with
matplotlib, but the only type I'm interested in (bars) will not work.

I tried the examples coming with matplotlib, but even those don't work.

Here is the example code which produces the above error:
#!/usr/bin/env python
# a stacked bar plot with errorbars
from pylab import *

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 = 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, menMeans, width, color='r', yerr=womenStd)
p2 = bar(ind, womenMeans, width, color='y',
bottom=menMeans, yerr=menStd)

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

show()


Anyone has matplotlib installed and gets the same error?
I need to create some charts for a project I do at work, so maybe there
are other modules out there creating charts??

Andi

--
homepage: http://www.the-grudge.net
ICQ: 9450091
Skype: thegrudge_2
Mozilla Thunderbird 1.5.0.5
Arch Linux
 
A

Andi Clemens

btw, these are the versions I'm using (Archlinux):

Python: 2.4.3 (python-2.4.3-3)
matplotlib: 0.87.4 (python-matplotlib-0.87.4-1)
PyGTK: 2.8.6 (pygtk-2.8.6-3)

Andi


Andi said:
Hi,

everytime I try to plot a bar with matplotlib I get the following error
message:
Traceback (most recent call last):
File "bar_stacked.py", line 13, in ?
p1 = bar(ind, menMeans, width, color='r', yerr=womenStd)
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line
1641, in bar
ret = gca().bar(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 2485,
in bar
xerr = asarray([xerr]*nbars, Float)
File "/usr/lib/python2.4/site-packages/Numeric/Numeric.py", line 134,
in asarray
return multiarray.array(a, typecode, copy=0, savespace=savespace)
TypeError: a float is required


So I guess it has something to do with Numeric.py, but I can't figure
out what the problem is. I tried to google but found nothing...

Anyone has the same problem? I can plot all kinds of graphics with
matplotlib, but the only type I'm interested in (bars) will not work.

I tried the examples coming with matplotlib, but even those don't work.

Here is the example code which produces the above error:
#!/usr/bin/env python
# a stacked bar plot with errorbars
from pylab import *

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 = 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, menMeans, width, color='r', yerr=womenStd)
p2 = bar(ind, womenMeans, width, color='y',
bottom=menMeans, yerr=menStd)

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

show()


Anyone has matplotlib installed and gets the same error?
I need to create some charts for a project I do at work, so maybe there
are other modules out there creating charts??

Andi

--
homepage: http://www.the-grudge.net
ICQ: 9450091
Skype: thegrudge_2
Mozilla Thunderbird 1.5.0.5
Arch Linux
 
J

John Machin

Andi said:
Hi,

everytime I try to plot a bar with matplotlib I get the following error
message:
Traceback (most recent call last):
File "bar_stacked.py", line 13, in ?
p1 = bar(ind, menMeans, width, color='r', yerr=womenStd)
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line
1641, in bar
ret = gca().bar(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line
2485, in bar
xerr = asarray([xerr]*nbars, Float)
File "/usr/lib/python2.4/site-packages/Numeric/Numeric.py", line 134,
in asarray
return multiarray.array(a, typecode, copy=0, savespace=savespace)
TypeError: a float is required


So I guess it has something to do with Numeric.py, but I can't figure
out what the problem is. I tried to google but found nothing...

Anyone has the same problem? I can plot all kinds of graphics with
matplotlib, but the only type I'm interested in (bars) will not work.

I tried the examples coming with matplotlib, but even those don't work.

Here is the example code which produces the above error:
#!/usr/bin/env python
# a stacked bar plot with errorbars
from pylab import *

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 = 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, menMeans, width, color='r', yerr=womenStd)

Call me crazy -- I know SFA about matplotlib -- but I'd expect anything
scientific to barf if given tuples of integers to play with, and
following the docs links matplotlib > pylab > bar we find:
"""
bar(*args, **kwargs)
BAR(left, height, width=0.8, bottom=0,
color='b', yerr=None, xerr=None, ecolor='k', capsize=3)
Make a bar plot with rectangles at
left, left+width, 0, height
left and height are Numeric arrays.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""

Which of us is missing what?

Cheers,
John
 
A

Andi Clemens

Sorry but english isn't my native language so I don't really get what
you are trying to tell me, it took me quite a long time to figure out
what SFA means (I guess it is Sweet Fanny Adams => nothing at all).

All examples were taken from the matplotlib-homepage but they won't work
on my installation and I don't know why.
You point out that "left and height are Numeric arrays", and that is
exactly what they are, so what am I missing?

I tried nearly 10 examples from different websites and tutorials and
none of them worked!
So I don't think I'm doing something wrong, or am I?

Andi

John said:
Call me crazy -- I know SFA about matplotlib -- but I'd expect anything
scientific to barf if given tuples of integers to play with, and
following the docs links matplotlib > pylab > bar we find:
"""
bar(*args, **kwargs)
BAR(left, height, width=0.8, bottom=0,
color='b', yerr=None, xerr=None, ecolor='k', capsize=3)
Make a bar plot with rectangles at
left, left+width, 0, height
left and height are Numeric arrays.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""

Which of us is missing what?

Cheers,
John

--
homepage: http://www.the-grudge.net
ICQ: 9450091
Skype: thegrudge_2
Mozilla Thunderbird 1.5.0.5
Arch Linux
 
T

Tim Heaney

Andi Clemens said:
everytime I try to plot a bar with matplotlib I get the following
error message:
Traceback (most recent call last):
File "bar_stacked.py", line 13, in ?
p1 = bar(ind, menMeans, width, color='r', yerr=womenStd)
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line
1641, in bar
ret = gca().bar(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line
2485, in bar
xerr = asarray([xerr]*nbars, Float)
File "/usr/lib/python2.4/site-packages/Numeric/Numeric.py", line
134, in asarray
return multiarray.array(a, typecode, copy=0, savespace=savespace)
TypeError: a float is required


So I guess it has something to do with Numeric.py, but I can't figure
out what the problem is. I tried to google but found nothing...

Anyone has the same problem? I can plot all kinds of graphics with
matplotlib, but the only type I'm interested in (bars) will not
work.

It looks like it doesn't like not having xerr set. The documentation
says

xerr and yerr, if not None, will be used to generate errorbars on
the bar chart

implying that if they are None (the default), then no errorbars will
be generated. But that doesn't seem to be the case. When I add an xerr
to both p1 and p2

p1 = bar(ind, menMeans, width, color='r', yerr=womenStd,
xerr=zeros(N))
p2 = bar(ind, womenMeans, width, color='y',
bottom=menMeans, yerr=menStd, xerr=zeros(N))

I get the following bar chart

http://img176.imageshack.us/my.php?image=imagenx6.png

with no errors.

Tim
 
A

Andi Clemens

THATS IT! Thanks!
But why are all the examples wrong? Maybe they changed the API?

Andi

Tim said:
Andi Clemens said:
everytime I try to plot a bar with matplotlib I get the following
error message:
Traceback (most recent call last):
File "bar_stacked.py", line 13, in ?
p1 = bar(ind, menMeans, width, color='r', yerr=womenStd)
File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line
1641, in bar
ret = gca().bar(*args, **kwargs)
File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line
2485, in bar
xerr = asarray([xerr]*nbars, Float)
File "/usr/lib/python2.4/site-packages/Numeric/Numeric.py", line
134, in asarray
return multiarray.array(a, typecode, copy=0, savespace=savespace)
TypeError: a float is required


So I guess it has something to do with Numeric.py, but I can't figure
out what the problem is. I tried to google but found nothing...

Anyone has the same problem? I can plot all kinds of graphics with
matplotlib, but the only type I'm interested in (bars) will not
work.

It looks like it doesn't like not having xerr set. The documentation
says

xerr and yerr, if not None, will be used to generate errorbars on
the bar chart

implying that if they are None (the default), then no errorbars will
be generated. But that doesn't seem to be the case. When I add an xerr
to both p1 and p2

p1 = bar(ind, menMeans, width, color='r', yerr=womenStd,
xerr=zeros(N))
p2 = bar(ind, womenMeans, width, color='y',
bottom=menMeans, yerr=menStd, xerr=zeros(N))

I get the following bar chart

http://img176.imageshack.us/my.php?image=imagenx6.png

with no errors.

Tim

--
homepage: http://www.the-grudge.net
ICQ: 9450091
Skype: thegrudge_2
Mozilla Thunderbird 1.5.0.5
Arch Linux
 
J

John Machin

Andi said:
All examples were taken from the matplotlib-homepage but they won't work
on my installation and I don't know why.
You point out that "left and height are Numeric arrays", and that is
exactly what they are, so what am I missing?

Looks to me like for "height" you are passing in menMeans which is a
tuple containing integers. The standard deviation args have the same
problem
I tried nearly 10 examples from different websites and tutorials and
none of them worked!

Care to point me at the smallest other example that didn't work? Did
you copy/paste the example or did you retype it? Does "not work" mean
each gave the same error from the same line of code in Numeric? If not,
is there any pattern that you can detect?
 
A

Andi Clemens

Tim Heaney found out what the problem is, thanks for your help!
;-)

Andi

John said:
Looks to me like for "height" you are passing in menMeans which is a
tuple containing integers. The standard deviation args have the same
problem


Care to point me at the smallest other example that didn't work? Did
you copy/paste the example or did you retype it? Does "not work" mean
each gave the same error from the same line of code in Numeric? If not,
is there any pattern that you can detect?

--
homepage: http://www.the-grudge.net
ICQ: 9450091
Skype: thegrudge_2
Mozilla Thunderbird 1.5.0.5
Arch Linux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top