bring to front in dual plot
显示 更早的评论
In dual plot, how to bring the left plot to front, I try uistack but didn't work
9 个评论
We see in testing that even with a final "yyaxis left" that the blue line initially drawn with yyaxis left is underneath the line drawn with yyaxis right.
x = linspace(0,1);
yyaxis left
plot(x, x);
yyaxis right
plot(x, sin(3*pi*x))
yyaxis left
I didn't say it brought it to the foreground, Walter, I said it gave it focus...
I suppose I should have gone on to say that afaik, there isn't any documented way to do that other than redraw the line (or decide a priori which is desired to be on top and draw the axes in that obverse order initially).
With two lines on one axis, one could use the old trick of rearranging the order of the children of the axes; they would be the lines and permuting the order rearranged the precedence.
With yyaxis, there are two axes, not just one and each has only the one line as its child; hence there's nothing that one can reorder to affect the precedence; they are in the order in which were drawn, last first.
One might think that one should be able to rearrange the two axes handles, but alas! that isn't allowed...
Duplicating your example
x = linspace(0,1);
yyaxis left
plot(x, x);
yyaxis right
plot(x, sin(3*pi*x))
yyaxis left
hAx=gca
hAx.YAxis=flipud(hAx.YAxis)
x = linspace(0,1);
yyaxis left
plot(x, x);
yyaxis right
plot(x, sin(3*pi*x))
yyaxis left
hold on
plot(x, x);
xlim([0.3 0.5]), ylim(xlim) % hone in on crossing
Now blue is back on top; why gave focus back to the lefthand axes...
Adam Danz
2025-7-24
You’re definitely not alone in wanting control over the stacking order with yyaxis. This is a limitation that’s come up quite a few times over the years, and I can understand how frustrating it can be.
- https://www.mathworks.com/matlabcentral/answers/489486-link-axes-with-different-y-scales
- https://www.mathworks.com/matlabcentral/answers/335430-plot-order-with-two-y-axes
- https://www.mathworks.com/matlabcentral/answers/1603690-how-to-change-the-order-of-plots
- https://www.mathworks.com/matlabcentral/answers/855500-uistack-giving-error-with-yyaxis
- https://www.mathworks.com/matlabcentral/answers/280015-draw-stack-order-for-yyaxis
- https://www.mathworks.com/matlabcentral/answers/1661295-how-to-set-front-and-back-lines-on-plot-with-yyaxis-left-and-right
- https://stackoverflow.com/questions/37709988/plot-line-chart-on-the-left-axis-uper-the-bar-chart-on-the-right-axis
@Adam Danz, just out of curiosity, if it were allowable to reverse the order of the two axes, would that have the desired side effect? (My guess is "not" as there most likely is no callback that would detect the change in order to trigger a redraw.)
No, there's more to it with yyaxis. But you're right that changing the child order can be a strategy for controling stacking order in general.
Just for fun:
yyaxis right
plot(1:5)
yyaxis left
plot(5:-1:1)

Let's look at the children.
yyaxis right
h1 = get(gca,'Children')
Note that the only child returned is the line on the right.
yyaxis left
h2 = get(gca,'Children')
Now the only child returned is the line on the left.
But....
h1.Parent == h2.Parent
@Adam Danz says "changing the child order can be a strategy for controling stacking order in general. "
Yeah, I learned about that from there having been a blog illustrating it at some distant time in the past; couldn't begin to say who or when, any more.
Your demo illustrates the point I made earlier about there being only one child/axes rather than multiple so that there isn't anything to sort/rearrange to affect stacking order.
My only thought was whether if one could apply the same concept to the axes order instead of lines it might just trigger a refresh of both axes.
yyaxis right
plot(1:5)
yyaxis left
plot(5:-1:1)
hAx=gca
hAx.YAxis
hAx.YAxis(1)==hAx.YAxis(2)
yyaxis is a special beastie, indeed; being one axes but with two selectable independent NumericRuler Y-axes.
For comparison, the venerable plotyy
x=1:5;
hAx=plotyy(x,flip(x),x,x)
hAx==gca
creates two completely independent axes, overlaying the second on top of the first.
Either way, there's no convenient way to change the stacking order; while one can
hAx=flip(hAx);
hAx==gca
and indeed, change the storage order ot the two axes since now the handle array is just a graphics object array like any other, that has no effect on the existing stacking order.
PS. I'm not sure what the warning is about; that doesn't happen locally, but I'm still with R2021b/R2022b here.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




