This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

matplotlib's approach to plotting functions requires you to compute the x and y vertices of the curves you want to plot and then pass it off to plot. Eg for a normal pdf, matplotlib.mlab provides such a function:

   1 from matplotlib.mlab import normpdf
   2 import matplotlib.numerix as nx
   3 import pylab as p
   4 
   5 x = nx.arange(-4, 4, 0.01)
   6 y = normpdf(x, 0, 1) # unit normal
   7 p.plot(x,y, color='red', lw=2)
   8 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 (as of 0.83). If you need this, please post to the mailing list or submit a sourceforge support request.

Rich Shepard was interested in plotting "S curves" and "Z curves", and a little bit of googling suggests that the S curve is a sigmoid and the Z curve is simply 1.0-sigmoid. There are many simple forms for sigmoids: eg, the hill, boltzman, and arc tangent functions. Here is an example of the boltzman function:

   1 import matplotlib.numerix as nx
   2 import pylab as p
   3 
   4 def boltzman(x, xmid, tau):
   5     """
   6     evaluate the boltzman function with midpoint xmid and time constant tau
   7     over x
   8     """
   9     return 1. / (1. + nx.exp(-(x-xmid)/tau))
  10 
  11 x = nx.arange(-6, 6, .01)
  12 S = boltzman(x, 0, 1)
  13 Z = 1-boltzman(x, 0.5, 1)
  14 p.plot(x, S, x, Z, color='red', lw=2)
  15 p.show()

See also sigmoids at mathworld.

People often want to shade an area under these curves, eg under their intersection, which you can do with the magic of numerix and the matplotlib fill function:

   1 import matplotlib.numerix as nx
   2 import pylab as p
   3 
   4 def boltzman(x, xmid, tau):
   5     """
   6     evaluate the boltzman function with midpoint xmid and time constant tau
   7     over x
   8     """
   9     return 1. / (1. + nx.exp(-(x-xmid)/tau))
  10 
  11 def fill_below_intersection(x, S, Z):
  12     """
  13     fill the region below the intersection of S and Z
  14     """
  15     #find the intersection point
  16     ind = nx.nonzero( nx.absolute(S-Z)==min(nx.absolute(S-Z)))[0]
  17     # compute a new curve which we will fill below
  18     Y = nx.zeros(S.shape, typecode=nx.Float)
  19     Y[:ind] = S[:ind]  # Y is S up to the intersection
  20     Y[ind:] = Z[ind:]  # and Z beyond it
  21     p.fill(x, Y, facecolor='blue', alpha=0.5)
  22 
  23 x = nx.arange(-6, 6, .01)
  24 S = boltzman(x, 0, 1)
  25 Z = 1-boltzman(x, 0.5, 1)
  26 p.plot(x, S, x, Z, color='red', lw=2)
  27 fill_below_intersection(x, S, Z)
  28 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.

sigmoids2.png


CategoryCookbookMatplotlib

SciPy: Cookbook/Matplotlib/SigmoidalFunctions (last edited 2015-10-24 17:48:23 by anonymous)