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([x], [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
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@lists.sourceforge.net

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