Hi Rich,
I'm CC-ing my reply to the matplotlib-users list, so other people can
contribute to and benefit from the discussion.
> John, Last evening I read through the user guide and
> tutorial for matplotlib. Although I've done my scientific
> plotting using Gri (a better gnuplot) for years, and more
> recently with PSTricks, the philosophy and syntax of
> matplotlib is quite easy to grasp. Congratulations on a
> masterful job!
Thanks!
> Despite my careful reading, I saw nothing about creating
> the types of curves I need to plot. A sample is attached. I
> need to be able to draw overlapping normal curves and S- and
> Z-curves at the ends of the range of x values. Can I do this
> with matplotlib, particularly by specifying end points, the
> x value where y=1.0, and the y value of the point of
> inflection?
> Plotting triangular and trapezoidal versions are easy as
> the connecting lines are straight. Doing the normal curves
> (complete and either half) is a challenge.
matplotlib's general approach is that you compute the x and y vertices
of the curves you want to plot and then pass it off to plot. So you
could write a helper function to generate these points given your
parameters of interest and then plot them. Eg for a normal pdf,
matplotlib.mlab provides such a function
from matplotlib.mlab import normpdf
import matplotlib.numerix as nx
import pylab as p
x = nx.arange(-4, 4, 0.01)
y = normpdf(x, 0, 1) # unit normal
p.plot(x,y, color='red', lw=2)
p.show()
Of course, some curves do not have closed form expressions and are not
amenable for such treatment. Some of the matplotlib backends have the
capability to draw arbitrary paths with splines (cubic and quartic)
but this functionality hasn't been exposed to the user yet. If you
need this, let me know and I can provide an interface in an upcoming
release.
I had not heard of "S curves" and "Z curves", but a little bit of
googling [1,2] suggests that the S curve is a sigmoid and the Z curve
is simply 1-sigmoid. If this is what you are looking for, there are
many simple forms for sigmoids: eg, the hill, boltzman, and arc
tangent functions. Here is an example of the boltzman function
import matplotlib.numerix as nx
import pylab as p
def boltzman(x, xmid, tau):
"""
evaluate the boltzman function with midpoint xmin and time constant tau
over x
"""
return 1. / (1. + nx.exp(-x-xmid)/tau)
x = nx.arange(-6, 6, .01)
S = boltzman(x, 0, 1)
Z = 1-boltzman(x, 0.5, 1)
p.plot(x, S, x, Z, color='red', lw=2)
p.show()
See also http://mathworld.wolfram.com/SigmoidFunction.html. With a
little arithmetic you can write a helper function that takes the
midpoint and saturation point as arguments and computes the relevant
parameters and points.
From the book jacket on your home page [3], I'll anticipate your next
question that you may want to fill the area below the intersection of
the S and Z curves, which you can do with the magic of numerix and the
fill function
import matplotlib.numerix as nx
import pylab as p
def boltzman(x, xmid, tau):
"""
evaluate the boltzman function with midpoint xmin and time constant tau
over x
"""
return 1. / (1. + nx.exp(-x-xmid)/tau)
def fill_below_intersection(x, S, Z):
"""
fill the region below the intersection of S and Z
"""
#find the intersection point
ind = nx.nonzero( nx.absolute(S-Z)==min(nx.absolute(S-Z)))[0]
# compute a new curve which we will fill below
Y = nx.zeros(S.shape, typecode=nx.Float)
Y[:ind] = S[:ind] # Y is S up to the intersection
Y[ind:] = Z[ind:] # and Z beyond it
p.fill(x, Y, facecolor='blue', alpha=0.5)
x = nx.arange(-6, 6, .01)
S = boltzman(x, 0, 1)
Z = 1-boltzman(x, 0.5, 1)
p.plot(x, S, x, Z, color='red', lw=2)
fill_below_intersection(x, S, Z)
p.show()
As these examples illustrate, matplotlib doesn't come with helper
functions for all the kinds of curves people want to plot, but
along with numerix and python, provides the basic tools to enable you
to build them yourself.
Hope this helps!
JDH
[1] http://www.nicholasgcarr.com/digital_renderings/archives/the_z_curve_and_it.shtml
[2] http://bdn.borland.com/article/0,1410,32411,00.html
[3] http://www.appl-ecosys.com/newstuff.html