## matplotlib-users

 [Matplotlib-users] plotting constant functions From: Davidlohr Bueso A. - 2006-09-05 17:53:05 ```Hi, I am having trouble graphing constant functions in matplotlib (a simple y = 2). Here is a simple test script I'm using: from pylab import * def f(x): return 2 x = arange(1, 4) plot(x, f(x)) show() When I run it, I get this error: Traceback (most recent call last): File "test", line 8, in ? plot(x, f(x)) File "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", line 2019, in plot ret = gca().plot(*args, **kwargs) File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 2106, in plot for line in self._get_lines(*args, **d): File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 304, in _grab_next_args yield self._plot_2_args(remaining, **kwargs) File "/usr/lib/python2.4/site-packages/matplotlib/axes.py", line 253, in _plot_2_args assert(iterable(y)) AssertionError Could anyone tell me what I'm doing wrong (I'm new to matplotlib btw)? Thanks! ```
 Re: [Matplotlib-users] plotting constant functions From: Matt - 2006-09-05 18:01:32 ```Use this to plot it: plot([x], [f(x)]) you need the square brackets. -Matt --- "Davidlohr Bueso A." wrote: > Hi, > > I am having trouble graphing constant functions in > matplotlib (a simple > y = 2). Here is a simple test script I'm using: > > from pylab import * > > def f(x): > return 2 > > x = arange(1, 4) > > plot(x, f(x)) > show() > > When I run it, I get this error: > > Traceback (most recent call last): > File "test", line 8, in ? > plot(x, f(x)) > File > "/usr/lib/python2.4/site-packages/matplotlib/pylab.py", > line > 2019, in plot > ret = gca().plot(*args, **kwargs) > File > "/usr/lib/python2.4/site-packages/matplotlib/axes.py", > line 2106, > in plot > for line in self._get_lines(*args, **d): > File > "/usr/lib/python2.4/site-packages/matplotlib/axes.py", > line 304, > in _grab_next_args > yield self._plot_2_args(remaining, **kwargs) > File > "/usr/lib/python2.4/site-packages/matplotlib/axes.py", > line 253, > in _plot_2_args > assert(iterable(y)) > AssertionError > > > Could anyone tell me what I'm doing wrong (I'm new > to matplotlib btw)? > > Thanks! > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support > web services, security? > Get stuff done quickly with pre-integrated > technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 > based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ```
 Re: [Matplotlib-users] plotting constant functions From: Eric Firing - 2006-09-05 18:01:33 ```Davidlohr Bueso A. wrote: > Hi, > > I am having trouble graphing constant functions in matplotlib (a simple > y = 2). Here is a simple test script I'm using: > > from pylab import * > > def f(x): > return 2 Try return 2*ones(shape(x)) The problem is that you are returning a scalar, not an array the same size as x; mpl plots arrays, not scalars. Eric ```