11
$\begingroup$

The code is as follows,

g1 = Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
   RegionFunction -> Function[x, ArcSin[2/3] < Sin[x] < ArcSin[1]], 
   PlotStyle -> Red, Filling -> Axis];
g2 = Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
   RegionFunction -> Function[x, ArcSin[1/3] < Sin[x] < ArcSin[2/3]], 
   PlotStyle -> Green, Filling -> Axis];
g3 = Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
   RegionFunction -> Function[x, ArcSin[0] < Sin[x] < ArcSin[1/3]], 
   PlotStyle -> Blue, Filling -> Axis];
g4 = Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
   RegionFunction -> Function[x, ArcSin[-1/3] < Sin[x] < ArcSin[0]], 
   PlotStyle -> Gray, Filling -> Axis];
g5 = Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
   RegionFunction -> 
    Function[x, ArcSin[-2/3] < Sin[x] < ArcSin[-1/3]], 
   PlotStyle -> Orange, Filling -> Axis];
g6 = Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
   RegionFunction -> Function[x, ArcSin[-1] < Sin[x] < ArcSin[-2/3]], 
   PlotStyle -> Brown, Filling -> Axis];
Show[{g1, g2, g3, g4, g5, g6}, PlotRange -> {{-2*Pi, 2*Pi}, {-1, 1}}]

enter image description here

What I get is this. The x axis is not at the right position, and some filling is not to the axis. What is wrong with my code?

$\endgroup$
  • $\begingroup$ The problem is that the filling is generated to fit within each individual plot range. So just set the PlotRange in each of the plots individually, rather than just in the Show command. That way, the filling will fill the whole common plot range. $\endgroup$ – Mark McClure Sep 23 '14 at 13:04
  • $\begingroup$ @MarkMcClure its AxesOrigin that's causing the issue. Show will usually combine the plot ranges of the graphics, but it takes AxesOrigin from the first graphics object. $\endgroup$ – rcollyer Sep 23 '14 at 13:08
  • $\begingroup$ @rcollyer Yes, I think you're right. I guess my approach fixes the problem precisely because it forces the AxesOrgin to be the same throughout. $\endgroup$ – Mark McClure Sep 23 '14 at 13:12
  • $\begingroup$ @MarkMcClure absolutely. I think this is a good illustration of what Show manages to do by itself and what it needs some extra hints at. $\endgroup$ – rcollyer Sep 23 '14 at 13:15
14
$\begingroup$

Show combines multiple Graphics objects together, but it only works after everything has already been processed. So, it has to make some judgements on how to combine the resulting options together, and for most it uses the options present in the first Graphics object. Looking at your original graphic, I suspect it is the first plot that causes the issues,

In[17]:= Options[g1, {PlotRange, AxesOrigin}]

Out[17]= {PlotRange -> {{-2 \[Pi], 2 \[Pi]}, {0.536887, 0.999999}}, 
 AxesOrigin -> {0, 0.52}}

As you can see, Plot is setting the PlotRange fairly high, but the issue is the AxesOrigin which is causing the fill to not go to the same point as the other graphs. To correct that, add AxesOrigin -> {0, 0} to all your plots, but it is not needed in Show. Then you get this,

enter image description here

Now time for overkill. As there are a number of options that are the same across the plots, it often pays to set them for all the plots, but if you use SetOptions you need to remember to restore them afterwards. So, I would use a custom environment:

ClearAll[BlockOptions];
SetAttributes[BlockOptions, HoldAll];
BlockOptions[f : {_Symbol, ___?OptionQ | {___?OptionQ}}, body_] := 
 BlockOptions[{f}, body]
BlockOptions[f : {{_Symbol, ___?OptionQ | {___?OptionQ}} ...}, 
  body_] :=
 With[{fcns = f[[All, 1]]},
  Internal`InheritedBlock[fcns,
   SetOptions @@@ f;
   body
   ]
  ]

where BlockOptions temporarily changes the options for you. (See this answer for the details of Internal`InheritedBlock). Then, your code becomes

Show@
 BlockOptions[
  {Plot, Filling -> Axis, PlotRange -> {-1, 1}},
  {
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
    RegionFunction -> Function[x, ArcSin[2/3] < Sin[x] < ArcSin[1]], 
    PlotStyle -> Red], 
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
    RegionFunction -> Function[x, ArcSin[1/3] < Sin[x] < ArcSin[2/3]],
     PlotStyle -> Green], 
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
    RegionFunction -> Function[x, ArcSin[0] < Sin[x] < ArcSin[1/3]], 
    PlotStyle -> Blue], 
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
    RegionFunction -> Function[x, ArcSin[-1/3] < Sin[x] < ArcSin[0]], 
    PlotStyle -> Gray], 
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
    RegionFunction -> 
     Function[x, ArcSin[-2/3] < Sin[x] < ArcSin[-1/3]], 
    PlotStyle -> Orange], 
   Plot[Sin[x], {x, -2*Pi, 2*Pi}, 
    RegionFunction -> Function[x, ArcSin[-1] < Sin[x] < ArcSin[-2/3]],
     PlotStyle -> Brown]
   }
  ]

giving the same result. I used PlotRange here, instead of AxesOrigin, as it gives the same result, and I could move it out of Show.

$\endgroup$
  • $\begingroup$ how to restore Options to default? $\endgroup$ – Algohi Sep 23 '14 at 15:28
  • 1
    $\begingroup$ @Algohi I'm confused. What are you asking about? BlockOptions restores the options on exit. $\endgroup$ – rcollyer Sep 23 '14 at 15:32
  • $\begingroup$ Thank you so much guys! Thank you! $\endgroup$ – Anna Le Sep 23 '14 at 15:57
  • $\begingroup$ @rcollyer when you set option let us say SetOptions[Plot, AxesOrigin -> {0, 0}], then you can plot perfectly as OP wants. now this option still there. so without kernel quit, how to reset plot options to default? $\endgroup$ – Algohi Sep 23 '14 at 17:08
  • $\begingroup$ @Algohi That's what BlockOptions is for, to reset the options to what they were. Without it, you have to first save the options as they are. Internal`InheritedBlock does this job for us by restoring any list symbol (Plot in this case) to its original state on exit. Otherwise you have to use another variable. But, if you haven't done that and if you don't remember what they were, originally, you can use a sub-kernel to reacquire what they were. $\endgroup$ – rcollyer Sep 23 '14 at 17:36
7
$\begingroup$

One approach is to fill each Plot to 0 rather than Axes and set AxesOrigin to {0, 0} in Show

g1 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
   RegionFunction ->
    Function[x, ArcSin[2/3] < Sin[x] < ArcSin[1]],
   PlotStyle -> Red,
   Filling -> 0];
g2 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
   RegionFunction ->
    Function[x, ArcSin[1/3] < Sin[x] < ArcSin[2/3]],
   PlotStyle -> Green,
   Filling -> 0];
g3 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
   RegionFunction ->
    Function[x, ArcSin[0] < Sin[x] < ArcSin[1/3]],
   PlotStyle -> Blue,
   Filling -> 0];
g4 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
   RegionFunction ->
    Function[x, ArcSin[-1/3] < Sin[x] < ArcSin[0]],
   PlotStyle -> Gray,
   Filling -> 0];
g5 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
   RegionFunction ->
    Function[x, ArcSin[-2/3] < Sin[x] < ArcSin[-1/3]],
   PlotStyle -> Orange,
   Filling -> 0];
g6 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
   RegionFunction ->
    Function[x, ArcSin[-1] < Sin[x] < ArcSin[-2/3]],
   PlotStyle -> Brown, Filling -> 0];
Show[{g1, g2, g3, g4, g5, g6},
 PlotRange -> {{-2*Pi, 2*Pi}, {-1, 1}},
 AxesOrigin -> {0, 0}]

enter image description here

$\endgroup$
6
$\begingroup$

Yet another approach is using a single Plot with ConditionalExpression

colors = {Brown, Orange, Gray, Blue, Green, Red};
Plot[Evaluate[ConditionalExpression[Sin @ x, #1 < Sin @ Sin @ x < #2] & @@@
             Partition[Range[-1, 1, 1/3], 2, 1]], {x, -2*Pi, 2*Pi},
     PlotStyle -> colors, BaseStyle -> Thick, Filling -> Axis]

enter image description here

Note: For Version 9 you need to explicitly give the filling colors, by adding the option

FillingStyle -> (#1 -> Directive[Opacity[.5], #2] & @@@ Transpose[{Range[6], colors}])

or changing the Filling specification to

Filling -> ({#1 -> {Axis, Directive[Opacity[.5], #2]}} & @@@ Transpose[{Range[6], colors}])
$\endgroup$
  • $\begingroup$ Impressive how you shortened this thing :) $\endgroup$ – eldo Sep 23 '14 at 16:28
  • $\begingroup$ @eldo, it is actually longer in V9 (you need to add the filling styles explicitly); in V10 filling collors are picked nicely from PlotStyle colors. $\endgroup$ – kglr Sep 23 '14 at 16:32
  • $\begingroup$ I'm glad some one did this. $\endgroup$ – rcollyer Sep 24 '14 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.