# Shading region under (or over) a curve in horizontal bands

It seems like this should be simple, but I have spent most of the day trying to find a similar example on the web and there appears to be nothing remotely similar. Since I cannot find a way to make this work, an MWE is going to be difficult to provide, so I hope I can give a good description of a problem which, if someone can get working, I can adapt to my particular application.

Suppose you plot 5*Sin[x] from 0 to 2Pi. Now, shade the region under the curve so that the region with y-values in (0,1] is one color, the region with y-values in (1,2] is another color, and so forth up through (4,5].

It seems to be a simple matter to change colors based on domain, but not so much with range. I've tried working with Filling, FillingStyle, LinearGradientFilling, Mesh, MeshStyle, MeshFunctions, ColorFunction, RegionFunction.

colors = ColorData[97] /@ Range[6];

Plot[Evaluate @ Prepend[5  Sin @ x][Range[0, 5]],
{x, 0, 2 Pi},
Filling -> MapThread[{# + 1 -> {{1}, {#2, White}}} &, {Range @ 6, colors}],
PlotStyle -> PadRight[{Automatic}, 7, None]]


• RegionPlot with MeshShading.
RegionPlot[{ 0 <= y <= 5*Sin[x]}, {x, 0, π}, {y, 0, 5},
MeshFunctions -> {#2 &}, MeshShading -> ColorData[97] /@ Range[5],
Mesh -> {Range[5]}, AspectRatio -> Automatic]


• ParametricPlot with MeshShading and MeshStyle.
ParametricPlot[{x, t*5  Sin[x]}, {x, 0, 2 π}, {t, 0, 1},
MeshFunctions -> {#2 &},
Mesh -> {{{0, None},
Sequence @@
Directive@{AbsoluteThickness[2], Opacity[1], White}}]}},
MeshShading -> {None, Red, Green, Yellow, Blue, Pink},
GridLines -> {None, Range[5]}]


The solution is not difficult but rather cumbersome. I will only do two layers: below y=2.5 and above this height and only from x=0 to x=Pi. It will give an idea of how to do it in a general case. Let us start from the intersection points:

x1 = FindRoot[5 Sin[x] == 2.5, {x, 0.5}][[1, 2]];
x2 = FindRoot[5 Sin[x] == 2.5, {x, 2.5}][[1, 2]];


With this obtained let us plot:

Plot[{If[x < x1 || x > x2, 5 Sin[x], Nothing],
If[x1 < x < x2, 5 Sin[x], Nothing],
If[x1 < x < x2, 2.5, Nothing]}, {x, 0, \[Pi]},
PlotStyle -> {Blue, Blue, LightRed},
Filling -> {1 -> {Bottom,
LightBlue}, {3 -> {Bottom, LightBlue}}, {2 -> {Bottom,
LightRed}}}]


This yields:

Have fun!

fill = Graphics[{ColorData[97] @ #, Rectangle[{0, #}]} & /@ Range[6],