8
$\begingroup$

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.

$\endgroup$

4 Answers 4

5
$\begingroup$
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]]

enter image description here

$\endgroup$
5
$\begingroup$
  • 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]

enter image description here

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

enter image description here

$\endgroup$
3
$\begingroup$

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:

enter image description here

Have fun!

$\endgroup$
3
$\begingroup$
fill = Graphics[{ColorData[97] @ #, Rectangle[{0, #}]} & /@ Range[6], 
   PlotRangePadding -> None];

Plot[5  Sin @ x, {x, 0, 2 Pi}, Filling -> {1 -> {0, {None, Texture[fill]}}}]

enter image description here

$\endgroup$

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.