Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Plot a function with matplotlib?

Reply
Thread Tools

Plot a function with matplotlib?

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-19-2012
I have matplotlib and iPython, and want to plot a function over an
equally-spaced range of points.

That is to say, I want to say something like this:

plot(func, start, end)

rather than generating the X and Y values by hand, and plotting a scatter
graph. All the examples I've seen look something like this:

from pylab import *
import numpy as np
t = arange(0.0, 2.0+0.01, 0.01) # generate x-values
s = sin(t*pi) # and y-values
plot(t, s)
show()


which is fine for what it is, but I'm looking for an interface closer to
what my HP graphing calculator would use, i.e. something like this:


plot(lambda x: sin(x*pi), # function or expression to plot,
start=0.0,
end=2.0,
)

and have step size taken either from some default, or better still,
automatically calculated so one point is calculated per pixel.

Is there a way to do this in iPython or matplotlib?


--
Steven
 
Reply With Quote
 
 
 
 
Alex van der Spek
Guest
Posts: n/a
 
      05-19-2012
On Sat, 19 May 2012 01:59:59 +0000, Steven D'Aprano wrote:

> I have matplotlib and iPython, and want to plot a function over an
> equally-spaced range of points.
>
> That is to say, I want to say something like this:
>
> plot(func, start, end)
>
> rather than generating the X and Y values by hand, and plotting a
> scatter graph. All the examples I've seen look something like this:
>
> from pylab import *
> import numpy as np
> t = arange(0.0, 2.0+0.01, 0.01) # generate x-values s = sin(t*pi) #
> and y-values
> plot(t, s)
> show()
>
>
> which is fine for what it is, but I'm looking for an interface closer to
> what my HP graphing calculator would use, i.e. something like this:
>
>
> plot(lambda x: sin(x*pi), # function or expression to plot,
> start=0.0,
> end=2.0,
> )
>
> and have step size taken either from some default, or better still,
> automatically calculated so one point is calculated per pixel.
>
> Is there a way to do this in iPython or matplotlib?


Not to my knowledge unless you code it yourself.

However in gnuplot (www.gnuplot.info)

gnuplot>>> set xrange[start:end]
gnuplot>>> foo(x)=mycomplicatedfunction(x)
gnuplot>>> plot foo(x)

or shorter still

gnuplot>>> plot [start:end] foo(x)

without the need to set the xrange in advance.

 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      05-19-2012
> I'm looking for an interface closer to
> what my HP graphing calculator would use, i.e. something like this:
>
>
> plot(lambda x: sin(x*pi), # function or expression to plot,
> start=0.0,
> end=2.0,
> )
>
> and have step size taken either from some default, or better still,
> automatically calculated so one point is calculated per pixel.
>
> Is there a way to do this in iPython or matplotlib?

I don't think there is, but using range and list comprehension you can write a little utility function that does that:

HTH
--
Miki Tebeka <(E-Mail Removed)>
http://pythonwise.blogspot.com

def simplot(fn, start, end):
xs = range(start, end+1)
plot(xs, [fn(x) for x in xs)])
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"pointlabel"-like function for Python: distribute text labels on a2-d scatter plot to avoid overlapping labels C Barrington-Leigh Python 1 09-12-2010 09:58 PM
Variable size plot symbols, variable hue plot colors in Python (MatPlotLib) ? Dr. Colombes Python 1 02-10-2005 08:49 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
how to plot audio input? Peter Wieser Java 0 05-10-2004 10:42 PM
Have I lost the plot??? Annette Kurten Computer Support 24 04-06-2004 10:42 PM



Advertisments