tags:

views:

490

answers:

2

When you plot things in Matlab, the most recently plotted data series is placed on top of whatever's already there. For example:

figure; hold on
plot(sin(linspace(0,pi)),'linewidth',4,'color',[0 0 1])
plot(cos(linspace(0,pi)),'linewidth',4,'color',[1 0 0])

Here, the red line is shown on top of the blue line (where they intersect). Is there any way to set "how deep" a line is drawn, so that you can plot things beneath what's already there?

+14  A: 

Use the uistack command. For example:

h1 = plot(1:10, 'b');
hold on;
h2 = plot(1:10, 'r');

will plot two lines with the red line plotted on top of the blue line. If you then do:

uistack(h1);

the blue line will be brought to the front.

b3
I didn't know about uistack. That's a good one! +1
Scottie T
Thanks, I didn't know this one either. And conversely, uistack(h2,'bottom') will send the red line to the bottom, which is exactly what I'm after.
Will Robertson
@Will - Good point. To the OP: There are some other input arguments to uistack that allow more sophisticated adjustments of the stacking order. You can learn about these by typing "help uistack" at the command line.
b3
+2  A: 

You can also accomplish this by setting the order of the children vector of the current axes. If you do the following:

figure; hold on
h1 = plot(sin(linspace(0,pi)),'linewidth',4,'color',[0 0 1]);
h2 = plot(cos(linspace(0,pi)),'linewidth',4,'color',[1 0 0]);
h = get(gca, 'Children');

you will see that h is a vector that contains h1 and h2. The graphical stacking order is represented by the order of the handles in h. In this example, to reverse the stacking order you could do:

h = flipud(h);
set(gca, 'Children', h);
b3
In the end, I think writing a function for doing this usefully would end up being a re-implementation of uistack :) Good point, though.
Will Robertson

related questions