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 个评论

Show us what you have done; we have to guess otherwise. You mean you used yyaxis, maybe? In that case
yyaxis left
gives it the focus.
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 =
Axes with properties: YAxisLocation: 'left' YAxis: [2×1 NumericRuler] YLim: [0 1] XLim: [0 1] XScale: 'linear' YScale: 'linear' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Use GET to show all properties
hAx.YAxis=flipud(hAx.YAxis)
Unable to set the 'YAxis' property of class ''Axes'' because it is read-only.
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...
dpb
dpb 2025-7-24
编辑:dpb 2025-7-24
@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')
h1 =
Line with properties: Color: [0.8667 0.3294 0] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 2 3 4 5] YData: [1 2 3 4 5] Use GET to show all properties
Note that the only child returned is the line on the right.
yyaxis left
h2 = get(gca,'Children')
h2 =
Line with properties: Color: [0.0667 0.4431 0.7451] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 2 3 4 5] YData: [5 4 3 2 1] Use GET to show all properties
Now the only child returned is the line on the left.
But....
h1.Parent == h2.Parent
ans = logical
1
dpb
dpb 2025-7-24
编辑:dpb 2025-7-25
@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 =
Axes with properties: YAxisLocation: 'left' YAxis: [2×1 NumericRuler] YLim: [1 5] XLim: [1 5] XScale: 'linear' YScale: 'linear' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Use GET to show all properties
hAx.YAxis
ans =
2×1 NumericRuler array: NumericRuler NumericRuler
hAx.YAxis(1)==hAx.YAxis(2)
ans = logical
0
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)
●
Warning: InteractionOptions is not supported for legacy exploration modes.
Warning: InteractionOptions is not supported for legacy exploration modes.
hAx =
1×2 Axes array: Axes Axes
hAx==gca
ans = 1×2 logical array
1 0
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
ans = 1×2 logical array
0 1
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 的更多信息

提问:

2025-7-23

编辑:

dpb
2025-7-26

Community Treasure Hunt

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

Start Hunting!

Translated by