32
$\begingroup$

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.

$\endgroup$
6
  • 1
    $\begingroup$ Indeed, 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}]. $\endgroup$ Commented Jan 18, 2012 at 9:09
  • $\begingroup$ If possible, could you maybe post the specific example that is troubling you? $\endgroup$ Commented Jan 18, 2012 at 9:10
  • 1
    $\begingroup$ Additionally, 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]. $\endgroup$ Commented Jan 18, 2012 at 9:13
  • $\begingroup$ This is described in the "possible issues" section of the Show documentation. $\endgroup$
    – Simon
    Commented Jan 18, 2012 at 9:29
  • $\begingroup$ @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. $\endgroup$
    – uli
    Commented Jan 18, 2012 at 9:32

2 Answers 2

23
$\begingroup$

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

$\endgroup$
9
  • 1
    $\begingroup$ I'd modify your last bit to Show[ListPlot[... , Op3, Op1], Plot[...]]; after all, Show[] options override. $\endgroup$ Commented Jan 18, 2012 at 9:52
  • $\begingroup$ What happens in the Plot[...] part? Plot[...,Op2,Op3] or just Plot[..., Op2]? $\endgroup$
    – uli
    Commented Jan 18, 2012 at 10:06
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Commented Jan 18, 2012 at 10:14
  • 1
    $\begingroup$ @J.M. I seems to me that I have to distinguish PlotOptions from GraphicsOptions. $\endgroup$
    – uli
    Commented Jan 18, 2012 at 11:30
  • 1
    $\begingroup$ @uli That is what I was trying to say in the first section of my answer $\endgroup$
    – Szabolcs
    Commented Jan 18, 2012 at 12:31
13
$\begingroup$

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

$\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.