10

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.

VividD
  • 3,660
  • 4
  • 26
  • 42
Lawerance
  • 517
  • 3
  • 13
  • 4
    Probably because Show takes options from the first argument which is different for those cases. – Kuba Mar 22 '14 at 22:16
  • 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.) – Szabolcs Mar 22 '14 at 23:22
  • 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." – Michael E2 Mar 22 '14 at 23:39
  • 1
    @Michael Your second citation looks like a mistake in the documentation: actually Show does not care about options in gi other than g1 (AFAIK). – Alexey Popkov Mar 23 '14 at 01:33
  • @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. – Michael E2 Mar 23 '14 at 01:55
  • @Michael If your explanation is correct, we have a bug in Show: according to the citation, it must ignore default option values in the gi when concatenating options. – Alexey Popkov Mar 23 '14 at 02:00
  • @AlexeyPopkov You're right, one way or the other. I guess I'm tired and not reading carefully. That slipped right past me. – Michael E2 Mar 23 '14 at 02:16
  • @Michael In any case, thank you for the citation. I did not think that the developers have in mind more correct approach than we currently have as default behavior of Show. It is worth to fill a bug report on this issue. – Alexey Popkov Mar 23 '14 at 02:46
  • @J.M. Similar questions are marked as a duplicate of the one you linked, (128). Should this one be? Or perhaps merged, given the upvotes on the answer? – Michael E2 Dec 27 '16 at 23:57
  • @Michael, marking this one as a dupe ought to be fine. Interestingly, Szabolcs answered both, but this can be excused as the older question was asked during this site's beta phase. – J. M.'s missing motivation Dec 28 '16 at 05:23

1 Answers1

16

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]

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263