How to Change the Staking Order of Lines from Back-to-Front to Front-to-Back?

356 views (last 30 days)
It seems that the order of staking lines in a graph is back-to-front, so blue line will be behind red line.
hor_line = [1,1,1];
ver_line = [0,1,2];
plot([0 1 2],hor_line ,[1,1,1],ver_line, 'LineWidth', 8);
Is there any way to change the default staking order of lines from back-to-front to front-to-back? (so, by the same code, I will get the following)
hor_line = [1,1,1];
ver_line = [0,1,2];
plot([0 1 2],hor_line ,[1,1,1],ver_line, 'LineWidth', 8);

Accepted Answer

Walter Roberson
Walter Roberson on 23 Sep 2018
"It seems that the order of staking lines in a graph is back-to-front, so blue line will be behind red line."
There are two choices of visibility order of graphics objects in R2015b and later. They are described in the blog https://blogs.mathworks.com/graphics/2014/11/04/sortmethod/
Those are the only choices. In particular, there is no way of configuring to say that the first line specified in one plot() call should be on top of the second, third, etc., line in the same plot() call. plot() is for creating 2D lines, and axes with only 2D lines default to childorder, and child order for a series of lines drawn in the same plot call is always that the later lines draw over top of the first, same as if you had done a series of plot() calls of individual lines. This is not a default you can change.
You can change the stacking order after you draw everything, such as
set(gca, 'Children', flipud(get(gca, 'Children')) )
  2 Comments
Rightia Rollmann
Rightia Rollmann on 24 Sep 2018
Thank! But a follow-up question: Is there any way to change the order of legends so that the second line (i.e., the line that is plotted later) appears first (with its corresponding line color beside it)?
Walter Roberson
Walter Roberson on 24 Sep 2018
When you do not pass a list of graphics handles into legend() then legend() respects ChildOrder -- so the first legend is associated with the last entry on ChildOrder, the second legend is associated with the second last entry in ChildOrder, and so on bottom to top in ChildOrder. Bottom to top because the bottom entry is the first entry that was plotted unless the ChildOrder was changed.
You can change this by passing a list of graphics handles in to legend.
h = plot(1:10,2:11,'r*-',1:10, (11:-1:2), 'g^--');
legend(flipud(h), 'green', 'red')

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!