# Combining many plots with Show produces wrong x-axis position and wrong filling

The code is as follows,

g1 = Plot[Sin[x], {x, -2*Pi, 2*Pi},
RegionFunction -> Function[x, ArcSin[2/3] < Sin[x] < ArcSin],
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 < 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],
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}}] 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?

• 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. Sep 23 '14 at 13:04
• @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. Sep 23 '14 at 13:08
• @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. Sep 23 '14 at 13:12
• @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. Sep 23 '14 at 13:15

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:= Options[g1, {PlotRange, AxesOrigin}]

Out= {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, 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]]},
InternalInheritedBlock[fcns,
SetOptions @@@ f;
body
]
]


where BlockOptions temporarily changes the options for you. (See this answer for the details of InternalInheritedBlock). 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],
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 < Sin[x] < ArcSin[1/3]],
PlotStyle -> Blue],
Plot[Sin[x], {x, -2*Pi, 2*Pi},
RegionFunction -> Function[x, ArcSin[-1/3] < Sin[x] < ArcSin],
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.

• how to restore Options to default? Sep 23 '14 at 15:28
• @Algohi I'm confused. What are you asking about? BlockOptions restores the options on exit. Sep 23 '14 at 15:32
• Thank you so much guys! Thank you! Sep 23 '14 at 15:57
• @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? Sep 23 '14 at 17:08
• @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. InternalInheritedBlock 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. Sep 23 '14 at 17:36

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],
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 < 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],
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}] 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] Note: For Version 9 you need to explicitly give the filling colors, by adding the option

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


or changing the Filling specification to

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

• Impressive how you shortened this thing :)
– eldo
Sep 23 '14 at 16:28
• @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.
– kglr
Sep 23 '14 at 16:32
• I'm glad some one did this. Sep 24 '14 at 12:53