1
$\begingroup$

I was recently experimenting in Mathematica and was combining graphics and a contour plot and discovered the order of elements in show matters. Why would the order matter here?

Remove[g,c];
g = Graphics [Arrow[{{0,0},{1,1}}],PlotRange->{{-5,5},{-5,5}}];
c = ContourPlot[x^2+y^2,{x,-5,5},{y,-5,5}];
{Show[g,c],Show[c,g]}

enter image description here

enter image description here

$\endgroup$
7
  • $\begingroup$ Options are taken form first Show element, that is why there is/isn't Frame. Arrow is behind ContourPlot in first Show. $\endgroup$
    – Kuba
    Jun 4, 2013 at 21:12
  • $\begingroup$ related - mathematica.stackexchange.com/q/25169/5478 $\endgroup$
    – Kuba
    Jun 4, 2013 at 21:22
  • $\begingroup$ You can see that the area is under the contour shading by setting ContourShading->None in the ContourPlot. $\endgroup$ Jun 4, 2013 at 21:27
  • 1
    $\begingroup$ One way to think about it: When you paint a house, which color do you see, the primer (first coat, white) or the final coat? And if the paint is not completely opaque, you might see color from the undercoat. $\endgroup$
    – Michael E2
    Jun 5, 2013 at 0:03
  • 1
    $\begingroup$ @Mark, it might be more convincing for OP if he uses something like ContourShading -> Opacity[1/2], I'd bet. $\endgroup$ Jun 5, 2013 at 1:21

2 Answers 2

6
$\begingroup$

When combining graphics with Show, the order matters, because Mathematica will proceed to paint one argument after another, starting with the first argument. They will basically be laid on top of each other. There are analogues in the comments, like painting a house. Another thing to remember is that, when you use Show to combine things, the options are always taken from the first object, overriding options (such as Axes) given in other objects.

$\endgroup$
-6
$\begingroup$

This is a recurring problem with Mathematica graphics that generates many questions. I'm surprised it's not marked duplicate. In any case the Presentations Application, which I sell for $50, was initially designed to simplify all of this. The plot would be made in the following manner.

<< Presentations`
Draw2D[
 {ContourDraw[x^2 + y^2, {x, -5, 5}, {y, -5, 5}],
  Red,
  Arrow[{{0, 0}, {1, 1}}]},
 Frame -> True,
 ImageSize -> 250]

enter image description here

You just draw one item after another. They are drawn in the order given. You can freely mix graphics items produced by plots and graphics primitives. Everything is a graphics primitive. The syntax of the various Draw statements is the same as the corresponding Mathematica Plot statement - except that only Options that affect the drawn primitives are used. Options that affect the overall graphic, such as Frame, ImageSize, AspectRatio ect., all go at the end. Options such as PlotPoints, MaxRecursion, ColorFunction would go in the ContourDraw statement. There is no need to combine separate plots with Show, or use Epilog, or use graphics level jumping to get primitives into plots.

I'm not certain why the posted second plot showed a Red arrow, but Red is nice so I used it.

$\endgroup$
5
  • $\begingroup$ "Hey look! I sell my product \$50! I'm not answering your question at all but eh, I need \$$." In case you didn't notice your advertisement is not really appreciated. Even by newbies like me :) $\endgroup$
    – Öskå
    Jan 12, 2014 at 21:14
  • $\begingroup$ @Oska I'm sorry, but you are completely wrong. It was an answer that shows how a better paradigm completely avoids the problem that the poster fell into. And $50 is trivial for a 37 MB Application. $\endgroup$
    – David Park
    Jan 12, 2014 at 21:22
  • $\begingroup$ Although I see no difference between your plot and the Show[c,g] one. Which makes me think that \$50 can be saved in that case. $\endgroup$
    – Öskå
    Jan 12, 2014 at 21:25
  • $\begingroup$ @Oska There is this difference. The Show paradigm did not work for him the first time and he had to experiment to find a method that would work. I don't know how much time he spent doing that and posting the question here, but if his time is worth anything it must have been a reasonable bite out of $50. And the code and approach is quite a bit different, even if you can't see it. $\endgroup$
    – David Park
    Jan 12, 2014 at 22:10
  • 2
    $\begingroup$ His question was "Why does Show require the graphics to be in a particular order". Answering by saying "Buy this $50 package I sell, which also requires the graphics to be in a particular order" is useless. Anon's answer explains what the OP had asked. $\endgroup$
    – rm -rf
    Jan 13, 2014 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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