Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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}]. –  J. M. Jan 18 '12 at 9:09
    
If possible, could you maybe post the specific example that is troubling you? –  J. M. Jan 18 '12 at 9:10
1  
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]. –  J. M. Jan 18 '12 at 9:13
    
This is described in the "possible issues" section of the Show documentation. –  Simon Jan 18 '12 at 9:29
    
@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 Jan 18 '12 at 9:32
show 1 more comment

2 Answers

up vote 11 down vote accepted

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

share|improve this answer
1  
I'd modify your last bit to Show[ListPlot[... , Op3, Op1], Plot[...]]; after all, Show[] options override. –  J. M. Jan 18 '12 at 9:52
    
What happens in the Plot[...] part? Plot[...,Op2,Op3] or just Plot[..., Op2]? –  uli Jan 18 '12 at 10:06
    
@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 Jan 18 '12 at 10:14
    
@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 Jan 18 '12 at 10:24
1  
@uli That is what I was trying to say in the first section of my answer –  Szabolcs Jan 18 '12 at 12:31
show 4 more comments

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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