[metapost] Plotting a Gaussian Distribution in MetaPost

Robert Jerrard rjerrard at math.concordia.ab.ca
Fri Nov 9 20:08:41 CET 2007


On Thu, 2007-08-11 at 23:07 +0100, Kevin Liu wrote:
> Hi,
> 
> I am currently learning MetaPost, which, thanks to the great tutorials, is going quite well. I am mostly using it to plot graphs of functions, which has also been going quite well so far. However, I am experiencing significant difficulties in plotting a normal distribution - The only output I have been able to achieve was quite confusing. 
> The graph package handles the task quite well, but it has limitations of its own. I would prefer an implementation directly in MetaPost to allow for maximum customisation.
> 
> Do you know of any solutions to this problem? Could this be a symptom of the very small numbers involved? Should I compute the numerical data and try to import it into MetaPost? Do you know of any bézier approximations of the gaussian distribution? I would be very grateful if you could help me out with this problem.
> 
> Thank you very much for your help
> 
> 
> Kevin

Hi Kevin, in Metapost I use the following definition to plot a Normal
probability density function with mean (mean) and standard deviation
(std) at the value of x (xvar).

def NormalPDF(expr mean, std, xvar)=
(2.718281828459045**(-0.5*((xvar-mean)/std)*(xvar-mean)/std))/(std*sqrt(2*3.141592653589793))
enddef;

then in the graph I ask to draw NormalPDF from some minimum x (xmin) to
some maximum x (xmax), in steps of dx, and I typically allow scaling in
the x and y directions with xscale and yscale:

beginfig(1);

set-up xmin, xmax, dx, xscale, yscale as numeric values

draw (xmin*xscale,NormalPDF(mean,std,xmin)*yscale){right}
  for ix=xmin+dx step dx until xmax:
    ..(ix*xscale, NormalPDF(mean,std,ix)*yscale)
  endfor;

draw other useful stuff like axes

endfig(1);

-- 
Dr. Robert J. Jerrard, Professor of Mathematics,
Concordia University College of Alberta,
7128 Ada Blvd., Edmonton, Alberta, T5B 4E4, Canada.
Phone: (780) 479-9291, Fax: (780) 474-1933.




More information about the metapost mailing list