Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2011-01-28 02:07:16 -0600

DSM gravatar image

Yep, x is iterable, as are the other arguments-- but that's not actually what the problem is. In this case the error message is a little deceptive, as it's actually what's inside y which is causing trouble. Let's look at the types involved:

sage: import numpy as np
sage: 
sage: x = np.arange(0, 5, 0.1)
sage: y = [e^(-i)+(random()-1/2)/10 for i in x]  
sage: ex = [random()/10 for i in y] 
sage: ey = [random()/10 for i in y] 
sage: for v in x,y,ex,ey:
....:     print len(v), type(v), type(v[0])
....:     
50 <type 'numpy.ndarray'> <type 'numpy.float64'>
50 <type 'list'> <type 'sage.symbolic.expression.Expression'>
50 <type 'list'> <type 'float'>
50 <type 'list'> <type 'float'>

And you see that the components of y aren't Python floats ('float') or numpy floats ('float64') [the difference between these two doesn't matter here] but Sage symbolic expressions, which (once again) matplotlib doesn't know what to do with.

Here the problem is caused by e:

sage: e
e
sage: type(e)
<type 'sage.symbolic.constants_c.E'>
sage: type(e^(-i))
<type 'sage.symbolic.expression.Expression'>

There are many ways around this problem: simply cast the array to floats, use "np.e", float(e), or (if you don't care about "symbolic" e) simply replace e, etc. The Sage plot functions automatically coerce to numerical values, as they should, but the matplotlib functions (possibly for speed reasons) don't do such a coercion.