Plot Option Precedence while combining Plots with Show[]
First a little background:
All of Mathematica's plotting functions produce a Graphics
expression (or Graphics3D
, but let's talk about Graphics
now). The Graphics
expression is simply a representation of what you see in the graphic. You can look at it by converting the output cell to InputForm
(Ctrl-Shift-I). For example, Plot
will produce Graphics
with Line
s in it.
Some of the options to plotting functions are passed on directly to Graphics
an affect its appearance (how its contents get rendered). An example is Axes
. Some others control what the plotting function will put into the graphics. Examples are PlotStyle
or PlotMarkers
. These are specific to (and different for) each plotting function.
How Show
works:
It combines several Graphics
expressions into one. The returned Graphics
expression will inherit its options from the first one passed to Show
. In Show
you can override some Graphics
options directly, but of course this will only override options for Graphics
, and not the plotting functions that produced the graphics (as those have already finished running by the time Show
sees their output).
So
Show[ListPlot[... , Op1], Plot[... , Op2], Op3]
is equivalent to
Show[ListPlot[... , Op3, Op1], Plot[...]]
or to
Show[ListPlot[...], Plot[...], Op3, Op1]
but this is only valid for ListPlot
options that are also Graphics
options. It is not valid for PlotMarkers
.
Also note that if the same option is specified several times in the same Graphics
, the first one takes precedence. (Thanks J. M.!)
Just to drive Szabolcs's points home, here are a few examples to demonstrate how Show[]
interacts with graphics objects and options given to it.
Let's start with
Show[Plot[Sin[x], {x, -1, 1}], Plot[Exp[x], {x, -2, 2}]]
Compare this image with the output from a version with options tweaked in places.
Show[Plot[Sin[x], {x, -1, 1}, PlotRange -> All], Plot[Exp[x], {x, -2, 2}]]
The same output is obtained from Show[Plot[Sin[x], {x, -1, 1}], Plot[Exp[x], {x, -2, 2}],
PlotRange -> All]
. On the other hand, Show[Plot[Sin[x], {x, -1, 1}], Plot[Exp[x], {x, -2, 2}, PlotRange -> All]]
gives the same output as the first version.
However,
Show[Plot[Sin[x], {x, -1, 1}], Plot[Exp[x], {x, -2, 2}],
PlotRange -> {{-3, 3}, All}]
and an explicit option setting in Show[]
, as expected, overrides the PlotRange
settings of the component graphics.
You don't really have to worry about
The lists of non-default options in the
g_i
are concatenated.
See, in functions like Plot[]
, the options, as with usual rules, are parsed left to right, and the first rule encountered that applies is the one taken. Thus, compare
Plot[Sin[x], {x, -2, 2}, MaxRecursion -> 1, PlotPoints -> 5, PlotPoints -> 20]
and
Plot[Sin[x], {x, -2, 2}, MaxRecursion -> 1, PlotPoints -> 20, PlotPoints -> 5]