MATLAB Answers


How to bring a plot to the front or back among multiple plots

Asked by Liqing
on 24 Feb 2012
Latest activity Edited by Mohammad Monfared on 24 Oct 2013

I have some interpolated ocean data that goes to land. I am thinking to first plot the interpolated values using 'scatter', and then plot the map using 'fill'. But the problem is that, it does not matter which one I plot first, the scatter plot is always on top. My question is how do I bring the scatter plot to back (behind the fill plot)?

Thank you,




No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 24 Feb 2012
 Accepted answer

Are you using transparency? Or does

get(gca, 'Renderer')

return OpenGL ?

If so then note that OpenGL does not pay attention to the child order: it works in (projected) Z order. It also has rules about what happens if there is a line and a surface in the same plane: the order is determined by the graphic type. I never bothered to remember which order OpenGL defines in this case, as I have encountered graphics driver that get the order exactly backwards.

In the case of OpenGL, you need to make sure that the item that you want to be on "top" has a higher (projected) Z coordinate, closer to the front from the perspective of the viewer. That can involve using scatter3() instead of scatter() and specifying a constant Z coordinate above or below the implied Z=0 of other objects.


That's a smart way of dealing with this issue. Thank you very much.

Walter!!! Thank you so much for this answer. This problem has been plaguing me for so long. I was working with multiple stacked objects and transparencies and I could never ever get the order right and also had to set the rendering to opengl. Going to go back and change the z-value for everything to insure the order is correct (before everything was z=0, so sometimes it would work and sometimes it wouldn't). The confusion came in because I was using images, and images have (I assume) a z=0 value, maybe I should use a pcolor plot instead so I can set the zvalue? I'll look into it. Thanks!!!

image() objects are 2D objects, not 3D objects whose Z happens to be 0. The difference between 2D and 3D with a constant Z can be seen when you tilt the perspective: when not seen from the top, the 2D object will vanish except for its edge, whereas the 3D object with constant Z will do the expected projection to become an angled surface.

pcolor is equivalent to surf() followed by view([0 90])

If you want to be able to tilt an image then you need to texture map it onto either a surface() object or a patch() object. Those steps are also required if you want to give an image a specific z coordinate so as to get layering as appropriate for your situation.

Answer by the cyclist
on 24 Feb 2012

The "Children" property of the axis is a list of the handles of the objects on the plot. In general, the order of that list determines the layering.

Here is a simple example where I manipulate the order of that list:

[EDITED in response to comment.]

hold on
fill([0.2 0.5 0.5 0.2],[0.2 0.2 0.5 0.5],'r')
hg = line([0 0.6],[0.6 0]);
h = get(gca,'Children');
set(gca,'Children',[h(3) h(2) h(1)])


Thank you very much.

I just tested it. It works well when there are only two plots. However, when I tried to plot something else (plot(x, y), it goes back again.

I edited my answer to provide an example with three objects.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!