3
$\begingroup$
Plot[PDF[NormalDistribution[70.5, 14.31], x], {x, -8, 130}, 
 PlotRange -> All, 
 PlotLabel -> "Normal Distribution with mean=70.5,std.dev=14.31"]
Probability[70.5 - 14.31 < x < 70.5 + 2 14.31, 
 x \[Distributed] NormalDistribution[70.5, 14.31]]

How can I make the image fill the color corresponding to the range of probability?

The effect is as follows:

enter image description here

$\endgroup$

3 Answers 3

4
$\begingroup$
Clear["Global`*"]

Using Manipulate

Manipulate[
 dist = NormalDistribution[m, s];
 pdf = PDF[dist, x];
 Show[
  Plot[pdf, {x, m - 4 s, m + 4 s},
   Frame -> True,
   FrameTicks -> {Automatic, {Automatic,
      Append[Flatten[
        Table[{
          {m + n*s, StringForm["μ+``σ", If[n == 1, "", n]]},
          {m - n*s, 
           StringForm["μ-``σ", If[n == 1, "", n]]}},
         {n, 1, 4}], 1], {m, "μ"}]}},
   Epilog -> {Text[StringForm["`` Pr\[ThinSpace]=\[ThinSpace]``",
       Lighter[ColorData[97][1], .75],
       Probability[
        m + interval[[1]]*s < x < m + interval[[2]]*s, 
        x \[Distributed] dist]],
      Scaled[{.8, .8}]]}],
  Plot[pdf, {x, m + interval[[1]]*s, m + interval[[2]]*s},
   Filling -> Axis,
   PlotRange -> {0, Automatic}],
  PlotRangePadding -> Scaled[.02],
  Axes -> True,
  AxesOrigin -> {m, 0},
  PlotLabel -> 
   StringForm["Normal Distribution with mean=``, std.dev=``\n",
    m, s]],
 {{m, 70.5, "μ"}, -100, 100, 0.5, Appearance -> "Labeled"},
 {{s, 14.31, "σ"}, 1, 20, 0.01, Appearance -> "Labeled"},
 {{interval, {-1, 2}}, -4, 4, 0.1,
  ControlType -> IntervalSlider,
  Method -> "Push",
  MinIntervalSize -> 0.1,
  Appearance -> "Labeled"}]

enter image description here

$\endgroup$
2
  • $\begingroup$ This image is really shocking! fantastic! But we can only calculate the probability of a closed interval. Can we modify it to calculate the probability of the left and right open intervals? For example, p (x>and x<). My idea is to make the code more universal $\endgroup$
    – csn899
    Mar 11, 2023 at 10:22
  • $\begingroup$ That is just 1 - Pr. That is a minor addition (another Text) that you should be able to add. If you encounter a problem that you cannot resolve, then post a new question showing what you have tried and explaining your difficulty. $\endgroup$
    – Bob Hanlon
    Mar 11, 2023 at 15:42
4
$\begingroup$

Try

Show[{
Plot[PDF[NormalDistribution[70.5, 14.31], x], {x, #1, #2}, 
PlotRange -> All, Filling -> Axis, AxesOrigin -> {0, 0}]
,
Plot[PDF[NormalDistribution[70.5, 14.31], x], {x, -8, 130}, 
PlotRange -> All,PlotLabel -> "Normal Distribution with mean=70.5,std.dev=14.31"]}, AxesOrigin -> {0, 0}
] &[70.5 - 14.31, 70.5 + 2 14.31]

enter image description here

$\endgroup$
4
  • $\begingroup$ The image can be automatically filled according to the probability corresponding to the calculated interval, such as p (x>and x<), and the image in the range of (x>and x<) can be automatically filled with color. My idea is to make the code more universal $\endgroup$
    – csn899
    Mar 11, 2023 at 10:37
  • $\begingroup$ @csn899 You asked for "How can I make the image fill the color corresponding to the range of probability?" My answer gives the plot, depending on the range, not meor! $\endgroup$ Mar 11, 2023 at 15:49
  • $\begingroup$ PlotLabel -> "Normal Distribution with mean=70.5,std.dev=14.31"Why is this label not displayed? $\endgroup$
    – csn899
    Mar 11, 2023 at 23:38
  • $\begingroup$ Try Show[{Plot[PDF[NormalDistribution[70.5, 14.31], x], {x, #1, #2}, PlotRange -> All, Filling -> Axis, AxesOrigin -> {0, 0}], Plot[PDF[NormalDistribution[70.5, 14.31], x], {x, -8, 130}, PlotRange -> All]}, AxesOrigin -> {0, 0}, PlotLabel -> "Normal Distribution with mean=70.5,std.dev=14.31"] &[ 70.5 - 14.31, 70.5 + 2 14.31] $\endgroup$ Mar 12, 2023 at 9:22
3
$\begingroup$
Clear["Global`*"]
dist = NormalDistribution[70.5, 14.31];
pdf = PDF[dist, x];
xl = Mean@dist - StandardDeviation@dist;
xr = Mean@dist + 2 StandardDeviation@dist;

max = First@Maximize[pdf, x];
prleft = (x /. Last@Maximize[pdf, x]) - 4.5 StandardDeviation@dist;
prright = (x /. Last@Maximize[pdf, x]) + 4.5 StandardDeviation@dist;

Show[
 Plot[pdf, {x, prleft, prright}
  , RegionFunction -> (xl < # < xr &)
  , Filling -> Axis
  , FillingStyle ->
   Nest[Lighter, ColorData[97, 1], 4]
  , PlotStyle -> None
  , PlotRange -> {
    {prleft, prright}
    , {0, max}
    }
  , PlotRangePadding -> Scaled[.05]
  , ImagePadding -> {{20, 10}, {20, 20}}
  , Ticks -> {Automatic, Automatic}
  , AspectRatio -> 7/10
  , Epilog -> {
    Dashed, Red
    , Line[{{xl, 0}, {xl, pdf /. x -> xl}}]
    , Line[{{xr, 0}, {xr, pdf /. x -> xr}}]
    }
  
  ]
 ,
 Plot[pdf, {x, prleft, prright}
  , PlotStyle -> Thick
  , AxesOrigin -> {prleft - 1, 0}
  
  ]
 , PlotLabel -> 
  StringForm["\nNormal Distribution with mean = ``, std.dev = ``", 
   Style[Mean@dist, Red], Style[StandardDeviation@dist, Blue]]
 ]

enter image description here

$\endgroup$
3
  • $\begingroup$ The image can be automatically filled according to the probability corresponding to the calculated interval, such as p (x>and x<), and the image in the range of (x>and x<) can be automatically filled with color. My idea is to make the code more universal $\endgroup$
    – csn899
    Mar 11, 2023 at 10:33
  • $\begingroup$ The image can be automatically filled... : How? $\endgroup$
    – Syed
    Mar 11, 2023 at 10:35
  • $\begingroup$ If the probability of a certain interval is required, only the endpoint value and inequality symbol can be input to determine the interval. Then the image of the corresponding interval will be automatically filled with shadows $\endgroup$
    – csn899
    Mar 11, 2023 at 12:04

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.