# How to shade bounded region of a plot

I have the following function

f[z_] := Piecewise[{{Cos[z] - 17 Sin[z]/z, z > 0}, {Cosh[z] - 17 Sinh[z]/z, z <= 0}}]

I am interested in the regions where $$\vert f(z)\vert \leq 1$$, I tried to shade that region using Filling, but my results were not the expected, how should I write the code? Here is my attempt:

Plot[{f[z], -1, 1}, {z, -5, 20}, PlotStyle -> {Automatic, Dashed, Dashed}, Filling -> {1 -> {3}}]


To be more precise: I want to shade the region $$D=\{(x,y):\vert f(x) \vert<1\text{ and } \vert y \vert <1 \}$$

• Is this not what you want: Filling -> {2 -> {3}} ? Apr 17, 2020 at 20:58
• does Plot[{f[z], ConditionalExpression[f[z], -1 < f[z] < 1], -1, 1}, {z, -25, 20}, PlotStyle -> {Automatic, None, Dashed, Dashed}, Filling -> {2 -> {{3}, Cyan}, 2 -> {{4}, Cyan}}, PlotPoints -> 100] give what you need?
– kglr
Apr 17, 2020 at 22:20
• .. or Plot[{f[z], ConditionalExpression[f[z], -1 < f[z] < 1], -1, 1}, {z, -25, 20}, PlotStyle -> {Automatic, None, Dashed, Dashed}, Filling -> {2 -> {Axis, Cyan}}, PlotPoints -> 100]?
– kglr
Apr 17, 2020 at 22:21
• @kglr Yes! That is what I wanted, Thanks! Apr 17, 2020 at 22:22

Plot[{f[z], ConditionalExpression[f[z], -1 < f[z] < 1], -1, 1}, {z, -25, 20},
PlotStyle -> {Automatic, None, Dashed, Dashed},
Filling -> {2 -> {{3}, Cyan}, 2 -> {{4}, Cyan}}, PlotPoints -> 100]


Also

Plot[{f[z], Clip[f[z], {-1, 1}, {Null, Null}], -1, 1}, {z, -25, 20},
PlotStyle -> {Automatic, None, Dashed, Dashed},
Filling -> {2 -> {{3}, Cyan}, 2 -> {{4}, Cyan}}, PlotPoints -> 100]


same picture

If the answer to my question in the comments is 'no', then perhaps this:

Plot[{f[z], Clip[f[z]], -1, 1}, {z, -25, 20},
PlotStyle -> {Automatic, None, Dashed, Dashed},
Filling -> {2 -> Axis}]


• This is not quite what I am looking for, I edited my question to be more precise regarding the region I wish to shade Apr 17, 2020 at 21:47