## [Matplotlib-users] Re: Can matplotlib draw normal curves

 [Matplotlib-users] Re: Can matplotlib draw normal curves From: John Hunter - 2005-07-28 14:45:34 ```>>>>> "Rich" == Rich Shepard writes: Hi Rich, I'm CC-ing my reply to the matplotlib-users list, so other people can contribute to and benefit from the discussion. Rich> John, Last evening I read through the user guide and Rich> tutorial for matplotlib. Although I've done my scientific Rich> plotting using Gri (a better gnuplot) for years, and more Rich> recently with PSTricks, the philosophy and syntax of Rich> matplotlib is quite easy to grasp. Congratulations on a Rich> masterful job! Thanks! Rich> Despite my careful reading, I saw nothing about creating Rich> the types of curves I need to plot. A sample is attached. I Rich> need to be able to draw overlapping normal curves and S- and Rich> Z-curves at the ends of the range of x values. Can I do this Rich> with matplotlib, particularly by specifying end points, the Rich> x value where y=1.0, and the y value of the point of Rich> inflection? Rich> Plotting triangular and trapezoidal versions are easy as Rich> the connecting lines are straight. Doing the normal curves Rich> (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 ```

 [Matplotlib-users] Re: Can matplotlib draw normal curves From: John Hunter - 2005-07-28 14:45:34 ```>>>>> "Rich" == Rich Shepard writes: Hi Rich, I'm CC-ing my reply to the matplotlib-users list, so other people can contribute to and benefit from the discussion. Rich> John, Last evening I read through the user guide and Rich> tutorial for matplotlib. Although I've done my scientific Rich> plotting using Gri (a better gnuplot) for years, and more Rich> recently with PSTricks, the philosophy and syntax of Rich> matplotlib is quite easy to grasp. Congratulations on a Rich> masterful job! Thanks! Rich> Despite my careful reading, I saw nothing about creating Rich> the types of curves I need to plot. A sample is attached. I Rich> need to be able to draw overlapping normal curves and S- and Rich> Z-curves at the ends of the range of x values. Can I do this Rich> with matplotlib, particularly by specifying end points, the Rich> x value where y=1.0, and the y value of the point of Rich> inflection? Rich> Plotting triangular and trapezoidal versions are easy as Rich> the connecting lines are straight. Doing the normal curves Rich> (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 ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: Ken McIvor - 2005-07-28 16:06:42 ```Rich Shepard wrote: > Now, my other current hangup is trying to identify what wxPython/wxWidget > widget to place in the UI for display of these plots. Working on this, too. > > Well, my plotting library is now set. I know I can do all these things in > PSTricks -- as far as drawing for publication is concerned -- but I need to > use them in computations, too. You have a few different options available to you for embedding matplotlib in wxPython: 1. Embed one of the wxPython backend widgets (which subclass wx.Panel) directly and draw plots on it using matplotlib's object-oriented API. This approach is demonstrated by some of the examples that come with matplotlib (examples/embedding_in_wx*.py). 2. Embed the PlotPanel from Matt Newville's `MPlot' package and draw plots on it using its plot() and oplot() methods. http://cars9.uchicago.edu/~newville/Python/MPlot/ 3. Embed the PlotPanel from my `wxmpl' module and draw plots on it using the matplotlib's object-oriented API. http://agni.phys.iit.edu/~kmcivor/wxmpl/ Each of these approachs has different benefits and drawbacks, so I encourage you to evaluate each of them and select the one that best meets your needs. Ken ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: Rich Shepard - 2005-07-28 16:16:10 ```On Thu, 28 Jul 2005, Ken McIvor wrote: > You have a few different options available to you for embedding matplotlib > in wxPython: > Each of these approachs has different benefits and drawbacks, so I encourage > you to evaluate each of them and select the one that best meets your needs. Ken, Thanks very much. I discovered matplotlib only yesterday so I have a lot of studying to do. I suspected that the OO API would be better for my needs than is the MatLab(TM)-style interface. Of course, I'm still brand-new to python and wxPython (I keep thinking in terms of C and GTK+), but I'm working hard at getting up to speed. I appreciate the pointers, Rich -- Dr. Richard B. Shepard, President | Author of "Quantifying Environmental Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic" ; Voice: 503-667-4517 Fax: 503-667-8863 ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: Chris Barker - 2005-07-28 16:16:31 ```John Hunter wrote: > I'm CC-ing my reply to the matplotlib-users list, so other people can > contribute to and benefit from the discussion. WOW! now that's support! John, Are you keeping a collection of all these little examples around? In a Wiki, perhaps? -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@... ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: Rich Shepard - 2005-07-28 16:35:52 ```On Thu, 28 Jul 2005, Chris Barker wrote: > Are you keeping a collection of all these little examples around? In a > Wiki, perhaps? Either a wiki or a tips-and-tricks page would be great. The advantage of the wiki is that those of us who do different things to extend or enhance matplotlib can post them there. The LyX wiki is a great example. Rich -- Dr. Richard B. Shepard, President | Author of "Quantifying Environmental Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic" ; Voice: 503-667-4517 Fax: 503-667-8863 ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: John Hunter - 2005-07-28 19:47:21 ```>>>>> "Chris" == Chris Barker writes: Chris> Are you keeping a collection of all these little examples Chris> around? In a Wiki, perhaps? Only in the mailing list archives... until now :-) wiki announcement to follow! JDH ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: Ken McIvor - 2005-07-28 16:50:51 ```Rich Shepard wrote: > Thanks very much. I discovered matplotlib only yesterday so I have a > lot of studying to do. I suspected that the OO API would be better for my > needs than is the MatLab(TM)-style interface. The matplotlib FAQ links to several resources that I found useful when learning about the OO API. http://matplotlib.sourceforge.net/faq.html#OO My experience was that reading classdocs was the most helpful source of information. The matplotlib.axes.Axes class is where most of the plotting methods live, so it's probably a good place to start, once you've figured out how to create Figures: http://matplotlib.sourceforge.net/matplotlib.axes.html#Axes The demos for my wxmpl module (demos/wxmpl-demos.py) may also help you out, as they are OO versions of several of the matplotlib examples. Look at all of the plot_XXX() functions at the beginning of the file. Since each of them takes a Figure as its only argument, they are backend-neutral. > Of course, I'm still brand-new to python and wxPython (I keep thinking in > terms of C and GTK+), but I'm working hard at getting up to speed. Ah, a glorious day! Our numbers grow! ;-) Ken ```
 Re: [Matplotlib-users] Re: Can matplotlib draw normal curves From: Rich Shepard - 2005-07-28 15:17:30 ```On Thu, 28 Jul 2005, John Hunter wrote: > I'm CC-ing my reply to the matplotlib-users list, so other people can > contribute to and benefit from the discussion. John, That's fine. I didn't want to post the pdf attachment to the list and I knew that you would have the greatest insight. > 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() That's a great example, thanks again. I also need to research the formula for these curves (d'oh!) because my need is not only to draw them but to calculate the y value for a given x value, and vice-versa. > 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. Splines are great graphically but not for generating plots from model output. > 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 Each specialty reassigns words to suit their own jargon: normal curve (math and statistics), bell curve (social sciences), Gaussian curve (physics) are all synonyms. The left and right halves (start at y=1.0, decrease sinusoidally to y=0.0) looks like an uppercase 'Z' if you use your imagination, while the right half looks like an uppercase 'S'. :-) > 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() Great! More research to define the curves I need and to understand what values I need to feed into the algorithm, and I'll be there. > 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. I suspected it was possible, but I hadn't seen the functions in the docs; I'll look at the NumPy docs again. (I need the eig function from to calculate the prinicpal eigenvector from a symmetrical matrix, but there's no plotting involved here.) >> 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 Actually, no. I don't need to fill the plots. That upper illustration is the fuzzy set intersect; the logical "AND" of two sets. The lower illustration shows "AND," "OR," and "X-OR." What I will need to be plotting, however, is the result of aggregating fuzzy sets using any of several methods (such as min-max). But, one step at a time as we re-write the code from the ground up in python (and wxPython). > 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. And I will be creating those Real Soon Now. And, I'll make them available to anyone who wants them. > Hope this helps! It certainly does; well beyond what I expected. Now, my other current hangup is trying to identify what wxPython/wxWidget widget to place in the UI for display of these plots. Working on this, too. Well, my plotting library is now set. I know I can do all these things in PSTricks -- as far as drawing for publication is concerned -- but I need to use them in computations, too. Again, thanks, Rich -- Dr. Richard B. Shepard, President | Author of "Quantifying Environmental Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic" ; Voice: 503-667-4517 Fax: 503-667-8863 ```