Plot order with two Y-axes?

조회 수: 20 (최근 30일)
peaksix
peaksix 2017년 4월 14일
댓글: Arthur Vieira 2021년 5월 5일
I am creating a plot with a line plot on the left y-axis and a bar plot on the right y-axis. I am doing this using:
line1 = plot(x,y);
yyaxis right
bar1 = bar(x,z);
This results in the bar plot on top of the line. So I tried:
uistack(bar1,'bottom')
Which gives me the error:
Children may only be set to a permutation of itself
The issue seems to be that they are not plotted on the same axis. Because if I omit the line "yyaxis right" (so that the line and bar plot both use the left y-axis, then uistack(bar1,'bottom') works correctly.
Is there any way to specify which y-axis is on top? I tried to accomplish this using the following code, which did not work:
yyaxis right
axes2 = gca;
uistack(axes2,'bottom');
This did not return an error, but it also did not work.

답변 (2개)

Ankitha Kollegal Arjun
Ankitha Kollegal Arjun 2017년 4월 18일
The issue seems to be with the 'yyaxis' command.
A possible workaround for this is using 'plotyy' instead of 'yyaxis'. However, please note that 'plotyy' will be deprecated soon. For the time being, this workaround should suffice.
Here is a sample code:
x=1:10;
y=x.^2;
z=x;
p = plotyy(x,y,x,z,'plot','bar');
line1 = p(1);
bar1 = p(2);
uistack(bar1,'bottom')
  댓글 수: 4
Adam Danz
Adam Danz 2019년 11월 14일
Just a few corrections to the demo.
  1. p (the 1st output to plotyy) is the 2 axis handles. To get the line object handes, use the 2nd and 3rd outputs.
  2. The demo is correct to change the stack of the axes but axis 2 (on top by default) is transparent while axis 1 has a white background by default. So, after the stack change, you will not see axis 2 unless you change the transparency of axis 1.
A demo of this is provided here:
Arthur Vieira
Arthur Vieira 2021년 5월 5일
+1 Indeed the right axis is usually of less importance and would sit better in the back. For now I will edit the *.svg later, before publishing the figures I'm producing.

댓글을 달려면 로그인하십시오.


Thomas Speh
Thomas Speh 2018년 7월 17일
No i don't think so.

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by