# Drawing a shaded area under a probability distribution

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:

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}],
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"}]


• 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 Mar 11, 2023 at 10:22
• 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. Mar 11, 2023 at 15:42

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]


• 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 Mar 11, 2023 at 10:37
• @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! Mar 11, 2023 at 15:49
• PlotLabel -> "Normal Distribution with mean=70.5,std.dev=14.31"Why is this label not displayed? Mar 11, 2023 at 23:38
• 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]  Mar 12, 2023 at 9:22
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}
}
, 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]]
]


• 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 Mar 11, 2023 at 10:33
• The image can be automatically filled... : How?
– Syed
Mar 11, 2023 at 10:35
• 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 Mar 11, 2023 at 12:04