Plot a function with matplotlib?

Discussion in 'Python' started by Steven D'Aprano, May 19, 2012.

  1. 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
    Steven D'Aprano, May 19, 2012
    #1
    1. Advertising

  2. 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.
    Alex van der Spek, May 19, 2012
    #2
    1. Advertising

  3. Steven D'Aprano

    Miki Tebeka Guest

    > 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 <>
    http://pythonwise.blogspot.com

    def simplot(fn, start, end):
    xs = range(start, end+1)
    plot(xs, [fn(x) for x in xs)])
    Miki Tebeka, May 19, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Hunter
    Replies:
    2
    Views:
    2,520
    Dr. Colombes
    Oct 20, 2004
  2. Dr. Colombes
    Replies:
    1
    Views:
    1,015
    John Hunter
    Feb 10, 2005
  3. Derek Basch

    Matplotlib logarithmic scatter plot

    Derek Basch, Feb 27, 2006, in forum: Python
    Replies:
    9
    Views:
    4,873
    evander21
    Nov 3, 2010
  4. N/A
    Replies:
    0
    Views:
    791
  5. Replies:
    1
    Views:
    344
    Alan Isaac
    Apr 13, 2007
Loading...

Share This Page