This plot function doesn't know about Sage functions and expects an argument that it can loop over, which is exactly what the error message is trying to say: it's expecting something it can iterate over (like a list of points), but the sage expression you pass isn't.

The "sometimes it works, sometimes it doesn't" effect is simply measuring whether or not you executed a reset() in some cell, setting plot back to the Sage one, after you executed the above import, which is why it appears so random-- apparently your cell execution order habits are kind of random. :^)

Keeping the matplotlib functions in their own space by using the "import matplotlib.pyplot as plt"/"plt.plot" approach -- which you do in one cell -- and avoiding either "from pylab import *" or "from matplotlib.pyplot import plot" is a better idea if you're going to want to mix Sage plots and matplotlib plots.

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:

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.

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.

Comments

Glad I could help. Since Sage involves a lot of different packages, sometimes it's hard to get them all talking to each other the way you might think they should. But instead of a thousand thanks, I'll settle for you clicking on the checkmark and accepting my answer. :^)