4
$\begingroup$

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:

enter image description here

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?

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – halirutan
    May 14, 2013 at 7:55
  • $\begingroup$ 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 $\endgroup$
    – Misery
    May 14, 2013 at 7:58
  • 3
    $\begingroup$ 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]]] $\endgroup$ May 14, 2013 at 7:59
  • 3
    $\begingroup$ You have to use PlotRange->All inside your second plot, not in the Show. $\endgroup$
    – halirutan
    May 14, 2013 at 8:06
  • $\begingroup$ 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! ;) $\endgroup$
    – Misery
    May 14, 2013 at 8:09

4 Answers 4

7
$\begingroup$

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]]

enter image description here

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:

examples of plotrange options

$\endgroup$
13
  • $\begingroup$ 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. $\endgroup$
    – halirutan
    May 14, 2013 at 7:52
  • $\begingroup$ 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! $\endgroup$
    – Misery
    May 14, 2013 at 7:56
  • 1
    $\begingroup$ 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?) $\endgroup$ May 14, 2013 at 8:03
  • 1
    $\begingroup$ @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. $\endgroup$
    – halirutan
    May 14, 2013 at 8:06
  • $\begingroup$ It was in new session of Mathematica so x didn't exist. But it is a vital suggestion. Thanks. $\endgroup$
    – Misery
    May 14, 2013 at 8:06
7
$\begingroup$

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.


Regarding your sarcastic comment:

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.

$\endgroup$
1
  • 1
    $\begingroup$ I figured it was just copying J.M.'s answer, hence the comment. Thank you sir! $\endgroup$ May 14, 2013 at 8:35
4
$\begingroup$

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}}]
$\endgroup$
1
  • $\begingroup$ Simple and smart. Like it. $\endgroup$
    – Trad Dog
    Jan 28, 2017 at 18:20
3
$\begingroup$

Another method:

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

ParametricPlot output

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}}, 
  MeshShading -> {Directive[Opacity[0.2], ColorData[1][1]], None}, 
  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}}, 
  MeshShading -> {None, Directive[Opacity[0.2], ColorData[1][1]]}, 
  AspectRatio -> 1/GoldenRatio, PlotRange -> All, Frame -> False]
 ]
$\endgroup$
2
  • $\begingroup$ @LCFactorization You'e welcome! and thank you very much, too. I like it, too (of course, I guess). :) $\endgroup$
    – Michael E2
    Nov 16, 2014 at 0:35
  • $\begingroup$ Sophisticated and smart. Like it too ! $\endgroup$
    – Trad Dog
    Jan 28, 2017 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.