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 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

share|improve this question
    
Options are taken form first Show element, that is why there is/isn't Frame. Arrow is behind ContourPlot in first Show. –  Kuba Jun 4 '13 at 21:12
    
related - mathematica.stackexchange.com/q/25169/5478 –  Kuba Jun 4 '13 at 21:22
    
You can see that the area is under the contour shading by setting ContourShading->None in the ContourPlot. –  Mark McClure Jun 4 '13 at 21:27
1  
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. –  Michael E2 Jun 5 '13 at 0:03
1  
@Mark, it might be more convincing for OP if he uses something like ContourShading -> Opacity[1/2], I'd bet. –  J. M. Jun 5 '13 at 1:21

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer
    
"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 :) –  Öskå Jan 12 at 21:14
    
@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. –  David Park Jan 12 at 21:22
    
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. –  Öskå Jan 12 at 21:25
    
@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. –  David Park Jan 12 at 22:10
    
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. –  rm -rf Jan 13 at 16:19

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.