10
$\begingroup$

I am wondering why there is a difference in the results from evaluating the two expressions, which differ only in the order of the arguments given to Show.

When I run

Animate[Show[spring[t], bob[t], traj], {t, 0, 20}]

the animation is different from

Animate[Show[traj, spring[t], bob[t]], {t, 0, 20}]

Parts of the plot get cut off, depending on the order of the arguments.

$\endgroup$
11
  • 4
    $\begingroup$ Probably because Show takes options from the first argument which is different for those cases. $\endgroup$
    – Kuba
    Mar 22, 2014 at 22:16
  • $\begingroup$ Thank you for posting the full code. I removed it to make the question easier to follow and more useful for future visitors, as this is a general problem and not related to the specific details of the code. (I haven't been able to find a good duplicate though one might exist.) $\endgroup$
    – Szabolcs
    Mar 22, 2014 at 23:22
  • $\begingroup$ From Show, "Details": "Show[g1, g2,...] or Show[{g1, g2,...}] concatenates the graphics primitives in the gi, effectively overlaying the graphics." And: "The lists of non-default options in the gi are concatenated." $\endgroup$
    – Michael E2
    Mar 22, 2014 at 23:39
  • 1
    $\begingroup$ @Michael Your second citation looks like a mistake in the documentation: actually Show does not care about options in gi other than g1 (AFAIK). $\endgroup$ Mar 23, 2014 at 1:33
  • $\begingroup$ @AlexeyPopkov I suppose it depends on what is meant. Consider p3 = Show[ p1 = Plot[x^2, {x, 0, 2}], p2 = Plot[2 Sin[x], {x, 0, 3}, ImageMargins -> 100] ]; AbsoluteOptions[p1] The first occurrence of an option overrides later ones, which are deleted. ImageMargins is set to 0. in p1, which would cause ImageMargins -> 100 to be ignored, IF that is how it works. In effect, then, what you say is true, too, since the first plot seems to set every possible option, or at least the documented ones. In any case, the doc. is at least misleading. $\endgroup$
    – Michael E2
    Mar 23, 2014 at 1:55

1 Answer 1

16
$\begingroup$

The order of arguments in Show makes a difference in two ways:

  1. The Graphics expression produced by Show will inherit options from the first argument

  2. The first argument will appear in the bottom layer, the last one in the top layer.

The most common problem (1) causes is that the plot range is inherited from the first argument, causing parts of the second one to be cut off. For example:

plot1 = Plot[x^2/20, {x, 0, 5}]
plot2 = Plot[Sin[x], {x, 0, 5}]

Show[plot1, plot2]

Show[plot2, plot1]

The simplest solution is to set the option explicitly in Show:

Show[plot1, plot2, PlotRange -> All]

$\endgroup$
2
  • $\begingroup$ +1 for the simple explanation. Does (2) applies to 3D graphics? $\endgroup$
    – Sigur
    Jun 2, 2014 at 23:42
  • $\begingroup$ @Sigur I don't think so. $\endgroup$
    – Szabolcs
    Jun 3, 2014 at 12:10

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