# plotting constant functions

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!

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

Use this to plot it:
plot(, [f(x)]) you need the square brackets.

-Matt

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",

···

--- "Davidlohr Bueso A." <dbueso@...1250...> wrote:

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