Title: Draw a scaled normal distribution in C#
The example Draw a normal distribution curve in C# shows how to draw a normal distribution. Unfortunately for some means and standard deviations, the result may not be very useful. For example, Figure 1 shows a normal distribution where the standard deviation is 3. The curve is so widely spread out that you can't really see anything useful.
This example scales its picture so the curve fills more of the available area. Enter the mean and standard deviation as before. The Deviations value indicates how many standard deviations you want to draw. For example, if you set this to 2, the program draws the graph with X coordinate centered on the mean and including X values up to 2 standard deviations to either side of the mean.
The key is the following section of code:
// Define the mapping from world
// coordinates onto the PictureBox.
float wxmin = mean  stddev * stddev_multiple;
float wxmax = mean + stddev * stddev_multiple;
float one_over_2pi = (float)(1.0 / (stddev * Math.Sqrt(2 * Math.PI)));
float wymax = F(mean, one_over_2pi, mean, stddev_multiple, var) * 1.1f;
float wymin = 0.2f * wymax;
This code determines the minimum and maximum X and Y coordinates that will be mapped onto the image the program produces. The code first sets wxmin and wxmax equal to the mean minus and plus the standard deviation times the number of standard deviations the user entered in the text box.
The standard deviation curve reaches its maximum above the mean, so the program calculates the value of that function over the mean. It multiples the result by 1.1 to allow a little room above the curve.
The code finishes by setting wymin equal to 0.2 times wymax. That allows some room below the X axis for X axis labels.
The rest of the code is similar to the previous example. See it for more details.
This example doesn't label the X and Y axes very well. A more elaborate example would try to draw and label tick marks with spacing that depends on the minimum and maximum X and Y values. I'll leave that as an exercise.
Download the example to experiment with it and to see additional details.
