Plot Option Precedence while combining Plots with Show[]

31

6

I like to build sophisticated plots by combining simpler ones with Show[]. Typically this involves setting non-default Plot-Options with the different Plot-Commands, like

Show[ ListPlot[ ,Op1], Plot[ ,Op2], Op3]

Unfortunately the Show[] command is not commutative, as

Show[ Plot[ ,Op2], ListPlot[ ,Op1], Op3]

can produce different results. My expectation was that putting settings in Op3 should overwrite the ones in Op1 and Op2 however this does not work with options like PlotMarkers which are only available within ListPlot[].

The description of the Show[g_1, g_2, g_3, ... ,g_i]-command gives only two hints:

Options explicitly specified in Show override those included in the graphics expression.

and

The lists of non-default options in the g_i are concatenated.

I’m not sure what this precisely means. Is

Show[ ListPlot[ ,Op1], Plot[ ,Op2], Op3]

equivalent to

Show[ ListPlot[ ,Union[Op1,Op2]], Plot[ ,Union[Op1,Op2]], Op3]?

while Op3 overwrites whatever is in Union[Op1,Op2]?

And there is one more question: In

Show[ g_1, g_2, g_3, ..., g_i ]

the Plot in g_1 seems to be treated specially as it defines the PlotRange for the final image generated.

I would like to know the full set of rules how the Plot-Options are combined and to which Plot or Plots they are applied.

uli

Posted 2012-01-18T09:01:18.990

Reputation: 1 275

1Indeed, the first graphic in Show[] is what delineates the PlotRange to be followed by the other graphics, in the absence of an explicit PlotRange setting for Show[]. Compare Show[Plot[Sin[x], {x, -1, 1}], Plot[Exp[x], {x, -2, 2}]] and Show[Plot[Sin[x], {x, -1, 1}], Plot[Exp[x], {x, -2, 2}], PlotRange -> {{-3, 3}, All}]. – J. M.'s ennui – 2012-01-18T09:09:11.613

If possible, could you maybe post the specific example that is troubling you? – J. M.'s ennui – 2012-01-18T09:10:17.587

1Additionally, options are parsed left to right. 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]. – J. M.'s ennui – 2012-01-18T09:13:06.927

This is described in the "possible issues" section of the Show documentation.

– Simon – 2012-01-18T09:29:54.093

@J.M. This question is on my long-standing list of open Mathematica questions. It is not connected to any specific plot. I have never been able to completely unravel the rules the govern the application of the PlotOptions. – uli – 2012-01-18T09:32:20.873

@Simon What is written in the "possible issues" section of the Show documentation, is at best misleading. The non-default options are of course the interesting ones. But the example given there is about the default PlotRange. – uli – 2012-01-18T09:35:59.403

Answers

23

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 Lines 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.!)

Szabolcs

Posted 2012-01-18T09:01:18.990

Reputation: 213 047

1I'd modify your last bit to Show[ListPlot[... , Op3, Op1], Plot[...]]; after all, Show[] options override. – J. M.'s ennui – 2012-01-18T09:52:14.773

What happens in the Plot[...] part? Plot[...,Op2,Op3] or just Plot[..., Op2]? – uli – 2012-01-18T10:06:09.680

@uli Nothing. As far as I can tell, Graphics-options are lost from it. This is because Graphics options usually have a default value in ListPlot even if they're not given explicitly. I've yet to find a Graphics option that will take effect in Plot (i.e. Show's second arg). Non-graphics options like PlotStyle of course do take effect. – Szabolcs – 2012-01-18T10:14:13.493

@Szabolcs My choice of ListPlot and Plot above was arbitrary. With "...options are lost..." you mean Show[ P_1[...,Op1], P_2[..., Op2], Op3] is taken as Show[ P_1[...,Op1,Op3], P_2[..., {}]]? – uli – 2012-01-18T10:24:46.577

@uli Yes, exactly. (By {} you mean "nothing", right?) – Szabolcs – 2012-01-18T10:27:34.423

@Szabolcs But with Show[Plot[x, {x, 0, 15}, PlotStyle -> Blue], Plot[x^2, {x, 0, 15}, PlotStyle -> Red]] the parabola is red, so Op2 is not lost. And yes, the empty list {} should indicate "nothing" :) – uli – 2012-01-18T10:41:11.120

@uli: that's as expected, since PlotStyle is not a Graphics[] option. The overrides happen for options that are both options of Show[] and the component graphics... – J. M.'s ennui – 2012-01-18T11:17:11.623

1@J.M. I seems to me that I have to distinguish PlotOptions from GraphicsOptions. – uli – 2012-01-18T11:30:39.747

1@uli That is what I was trying to say in the first section of my answer – Szabolcs – 2012-01-18T12:31:19.853

14

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}]]

sine and exponential, all defaults

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}]]

sine and exponential, first plot modified

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}]

sine and exponential, option override

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]

sine, rough plot

and

Plot[Sin[x], {x, -2, 2}, MaxRecursion -> 1, PlotPoints -> 20, PlotPoints -> 5]

sine, smooth plot

J. M.'s ennui

Posted 2012-01-18T09:01:18.990

Reputation: 115 520