How to plot filling under a curve?

I want to mark critical areas for statistical test on the plot. How can I do this? This:

pdf = PDF[NormalDistribution[], x]
Show[Plot[pdf, {x, -5, 5}],
Plot[pdf, {x, -5, -2.001}, Filling -> Axis]]


gives:

I know that it is probably some super-duper Mathematica feature, but as far as I know this filling shouldn't be cut in such strange way, so I don't want to use this feature. How can I make the filling look right?

-
Look at your second plot separately and you'll see it is cut too. Why should it be drawn completely when you combine the two graphs? Try PlotRange->All in your second graph which ensures that the whole curve is plotted. –  halirutan May 14 '13 at 7:55
PlotRange->All does not work either. But it works very well for x in <-4,4>. Mathematica knows which numbers are inappropriate! And if it decided that {-5,5} is wrong, and should cut the plot, it is right :p –  Misery May 14 '13 at 7:58
looking at @J.M.'s answer in mathematica.stackexchange.com/questions/18964/… you could also do (avoiding show): With[{dist = PDF[NormalDistribution[], #] &}, Plot[{ConditionalExpression[dist[x], x < -2], dist[x]}, {x, -5, 5}, Filling -> {1 -> Axis}, PlotStyle -> ColorData[1, 1]]] –  Pinguin Dirk May 14 '13 at 7:59
You have to use PlotRange->All inside your second plot, not in the Show. –  halirutan May 14 '13 at 8:06
Thanks. But still Mathematica takes my breath away. It even knows that when I want to plot something, it inteligently assumes that i don't want to plot it in the whole. Wow! ;) –  Misery May 14 '13 at 8:09

The reason this isn't working is because the Plot where you have the fill only plots up to a y-value of around 0.03 (try it on its own, without the second plot which shows the whole range) which means that you don't have a fill all the way to the top of where it appears in the graph. You can correct this by forcing it to plot to a higher value of y using the PlotRange option. You can either specify exactly the PlotRange you want, or simply give it the option All.

pdf = PDF[NormalDistribution[], x]
Show[Plot[pdf, {x, -5, 5}],Plot[pdf, {x, -5, -2.001}, Filling -> Axis,
PlotRange -> All]]


Note that where you put the PlotRange option is important. If you don't include it in the filled graphic then that graphic will be evaluated in the standard way and thus won't include the information for the filling up to the point you want. If you include the PlotRange option as an option in Show, or as an option in the first plot then it will not effect the information coming from the filled plot.

In general the non-default options for Show are taken from the graphics elements from the first one on. If the first element has PlotRange->{0,3} and the second has PlotRange->{0,4}, then the option for the whole graphics object will be PlotRange->{0,3}. It is worth playing with the different values for the PlotRanges in the following to see what is happening:

Show[Plot[x^2, {x, 0, 5}, PlotRange -> {0, 4}],Plot[x^3, {x, 0, 5}, PlotRange -> {0, 3}], PlotRange -> {0, 5}]


This particular set of options will give you the following plot but if you alter the values in PlotRange above, you will see how Show is taking these options. Try also without the final PlotRange to see which option it takes and how the graph looks:

-
Without explaining why it works now, the answer is useless for inexperienced users of Mathematica. If you don't know the reason for sure, only the fix, then give it a shot. You probably get corrected by the community, but you definitely learned something yourself. –  halirutan May 14 '13 at 7:52
No! It is very useful! Mathematica just has so many features, and is so inteligent... and so on. According to the saying: "It's not a bug! It's a feature". I learned its philosophy by heart and now I am happy that I spent so much money for this... ekhm miracle of software engineering! –  Misery May 14 '13 at 7:56
using this approach, you better Clear@x before defining pdf (or use pdf := PDF[NormalDistribution[], #] & and then pdf[x] in the plots) (or is there an easier way?) –  Pinguin Dirk May 14 '13 at 8:03
@Misery I was referring to other users finding this question and hoping for an explanation of the behavior. Assume a slightly more difficult problem and having a more complex solution without any explanation. –  halirutan May 14 '13 at 8:06
It was in new session of Mathematica so x didn't exist. But it is a vital suggestion. Thanks. –  Misery May 14 '13 at 8:06

Putting Pinguin Dirk's comment into an answer:

With[{dist = PDF[NormalDistribution[]]},
Plot[{If[x < -2, dist[x]], dist[x]}, {x, -5, 5},
Filling -> {1 -> Axis},
PlotStyle -> ColorData[1, 1]
]
]


Observe that ConditionalExpression is not needed as a simple and concise If is sufficient, and If also works in version 7 which the former does not.

But still Mathematica takes my breath away. It even knows that when I want to plot something, it inteligently(sic) assumes that i don't want to plot it in the whole. Wow! ;)

If you do not like the narrowed range you can use PlotRange -> Full to show the full plot range even if it is empty space. You can also use SetOptions to make this setting the default for one or more functions.

-
I figured it was just copying J.M.'s answer, hence the comment. Thank you sir! –  Pinguin Dirk May 14 '13 at 8:35

Here is another solution. The idea is constructing another function (Piecewise[{{0, x <= -2}}, pdf]) for pdf to fill, then you can fill the plot in a common style.

pdf = PDF[NormalDistribution[], x];
Plot[{pdf, Piecewise[{{0, x <= -2}}, pdf]}, {x, -5, 5}, Filling -> {1 -> {2}}]

-

Another method:

With[{pdf = PDF[NormalDistribution[]], cv = -2.001},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{cv}},
AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]
]


Two-tailed:

With[{pdf = PDF[NormalDistribution[]], cv = 2.001},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{-cv, cv}},
AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]
]


Right-tailed (note the reversal of MeshShading):

With[{pdf = PDF[NormalDistribution[]], cv = 2.001},
ParametricPlot[{x, t pdf[x]}, {x, -5, 5}, {t, 0, 1},
MeshFunctions -> {#3 &}, Mesh -> {{cv}},