views:

174

answers:

2

I need to plot a normal distribution and then shade some specific region of it. Right now I'm doing this by creating a plot of the distribution and overlaying it with a RegionPlot. This is pretty convoluted and I'm certain there must be a more elegant way of doing it. I Googled, looked at the docs, found nothing. Help me SO!

I guess Mathematica counts as programming? :D

+5  A: 

The easiest approach I can think of is to use two Plot functions, where one plots the range you want shaded, and the other one plots the entire range, while using the Filling option to get the shading. Then you display them together using Show, like so:

distFn = PDF[NormalDistribution[], x];
Show[
   {Plot[distFn, {x, -5, 5}],
    Plot[distFn, {x, -1, 1}, Filling -> {1 -> {0, Automatic}}]},
   PlotRange -> All]

It's still a little on the clunky side, but it works, and it should be easy enough to abstract into a single function if you do it a lot.

Pillsy
while "clunky" it is also very flexible in that you can vary the range that is filled in very easily. The same technique, of course, can be used where you need to plot something more complicated.
rcollyer
+1  A: 

It can also be done with a single Plot statement.

mu = 4; sigma = 3;

distFn = PDF[ NormalDistribution[mu, sigma], x];

Plot[Evaluate[distFn* {1, Boole[mu - sigma < x < mu + sigma]}], {x, mu - 3 sigma, mu + 3 sigma}, Filling -> {2 -> Axis}]

Bob Hanlon