Plot a function with matplotlib?

Vlastimil Brom vlastimil.brom at gmail.com
Sat May 19 07:45:06 EDT 2012


2012/5/19 Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list


Hi,
would a mpmath solution be acceptable?
http://code.google.com/p/mpmath/
http://mpmath.googlecode.com/svn/trunk/doc/build/plotting.html#mpmath.plot

"""
mpmath.plot(ctx, f, xlim=[-5, 5], ylim=None, points=200, file=None,
dpi=None, singularities=[], axes=None)
Shows a simple 2D plot of a function ... or list of functions ...
over a given interval specified by xlim. ...
"""

>>> import mpmath
>>> mpmath.plot(lambda x: mpmath.sin(x*mpmath.pi), xlim=[0.0, 2.0])

hth,
  vbr



More information about the Python-list mailing list