Hello, I'm trying to create an overlaid bar plot using yyaxis. I cannot figure out how to change the stacking order of the two axis objects (and their constituents). The default behavior seems to be that the right yyaxis and its children are above the left yyaxis. Due to the content of my plot, I'd prefer the opposite.
For example, the following script generates two bar plots that are overlaid. One is taller and wider and I'd like it to appear in the background but with its axis on the right side.
backgroundData = [100 75 80 150];
foregroundData = [10 30 25 64];
yyaxis left
bar(foregroundData,'FaceColor','r')
ylabel('Foreground Axis')
yyaxis right
bar(backgroundData,1.0,'FaceColor','b')
ylabel('Background Axis')
I've experimented with uistack, but can't get anything to stick. Of course swapping the left and right axes fixes the issue but I'd rather not do that. Thanks for any insight you can provide.

 채택된 답변

Charles Brown
Charles Brown 2016년 4월 20일

1 개 추천

I have created a work-around using manually-created axes:
backgroundData = [100 75 80 150];
foregroundData = [10 30 25 64];
% This works because ax1 is created AFTER ax2!
ax2 = axes('Position',[0.1300 0.1100 0.7750 0.8150]);
ax1 = axes('Position',[0.1300 0.1100 0.7750 0.8150]);
% Plotting the two bars on their axes. The order here doesn't matter.
bar(ax2,backgroundData,1.0,'FaceColor','b')
bar(ax1,foregroundData,.3,'FaceColor','r')
% Need to move the Y-axis after plotting the foreground data for some
% reason.
ax2.YAxisLocation = 'right';
ylabel(ax2,'Background Axis')
ylabel(ax1,'Foreground Axis')
% Now we have to set the background of the front axis to be transparent.
set(gca, 'Color', 'None')

추가 답변 (2개)

Kouichi C. Nakamura
Kouichi C. Nakamura 2021년 4월 23일

5 개 추천

Just to make it more noticeable, I copy the example here:
days = 0:5:35;
conc = [515 420 370 250 135 120 60 20];
temp = [29 23 27 25 20 23 23 17];
yyaxis right
b = bar(days, temp, 'FaceColor', [0.8 0.8 0.8]);
yyaxis left
p = plot(days, conc, 'LineWidth', 2);
This is the trick!
set(gca, 'SortMethod', 'depth')

댓글 수: 4

舞穹 赵
舞穹 赵 2021년 9월 13일
Wow, this answer is perfect and saves me a lot of efforts!
Kouichi C. Nakamura
Kouichi C. Nakamura 2021년 9월 13일
Thanks!
Calvin He
Calvin He 2022년 12월 1일
The problem is if you have multiple plots for the left axis, this reverses the stacking on those, and addes a white border around them.
I am trying to move the dotted gray lines to the back (plotted in yyaxis right)
set(gca,'sortmethod','depth') does this:
Shirley
Shirley 2025년 3월 18일
I'm still having issues if I am trying to plot two bar graphs using yyaxis. I cannot get the bar graph on the left axis to plot in the foreground. This is using R2024a. (note however I can get this to work for line graphs, just not bar graphs). Is anyone else seeing this same issue?

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

Mike Garrity
Mike Garrity 2016년 4월 20일

1 개 추천

I'm afraid that feature didn't make it into this initial release of yyaxis. As you've noted, the contents of the right side are always on top of the contents of the left side.
I'd be interested in knowing where you first looked when you were trying to figure out how to switch it. , because we've been trying to figure out what the most natural API would be for this.

댓글 수: 13

Thanks for the quick reply, Mike. My first instinct was to look for a handle associated with each yyaxis group. I could then use uistack to move the handles forward or backward. I haven't spent a whole lot of time using uistack, so it's possible there is a more consistent way to do this. I was imagining something like the following:
backgroundData = [100 75 80 150];
foregroundData = [10 30 25 64];
leftAxisHandle = yyaxis left
bar(foregroundData,'FaceColor','r')
ylabel('Foreground Axis')
rightAxisHandle = yyaxis right
bar(backgroundData,1.0,'FaceColor','b')
ylabel('Background Axis')
uistack(leftAxisHandle,'top')
Charles Brown
Charles Brown 2016년 4월 20일
And actually, the most natural interface for those unfamiliar with uistack would be for the order in which yyaxis is called to change the stack order. That is, if yyaxis right is invoked before yyaxis left, the right axis and its constituents would be drawn below the left axis. This would mirror the behavior of hold and other functions people are used to when attempting to overlay graphics.
Your notation is incorrect in 2016b. yyaxis does not have any arguments out.
hAxisR = yyaxis right
Error: Unexpected MATLAB expression.
hAxisR = yyaxis( gca, 'right')
Error using yyaxis
Too many output arguments.
Dani
Dani 2017년 5월 28일
As far as I see it is still not possible to change the drawing order or to which side the grid lines relate, even in 2017a? Is this correct Mike Garrity? Makes yyaxis completely useless for many applications. Maybe the warning should be "yyaxis is not recommended. Use plotyy instead."
Michael George
Michael George 2018년 2월 6일
It is very disappointing that this problem has not yet been fixed!
The workaround is messy. We expect a more elegant solution from the Mathworks team!
I agree with Charles Brown's suggestion above. Reordering plot with uistack should work as it does with other plots that don't have 2 y-axes. As well as the more intuitive option of simply plotting one axis first. If the low level interface for re-ordering the plots is not the same, then at least provide a different function for doing it!
Christopher Hoen
Christopher Hoen 2018년 2월 28일
편집: Christopher Hoen 2018년 2월 28일
There is another related issue which also is somewhat annoying. The grid-lines for the Y-axes are always connected to the LEFT Y-axis, no matter what is the active Y-axis when turning the YGrid and YMinorGrid properties 'on'. Thus you cannot get the proper Y-axis grid for the upper layer data.
Myself and Matlab
Myself and Matlab 2018년 6월 28일
Is the problem with yyaxis and uistack to be fixed?
Jake Tuttle
Jake Tuttle 2018년 7월 18일
I agree with Michael George. This is ridiculous that after 2 years this hasn't been fixed by the Mathworks team. It should be as simple as the order the plots are constructed. Why is this so difficult???
Bradley Stiritz
Bradley Stiritz 2018년 8월 1일
+1
>the contents of the right side are always on top of the contents of the left side.
I just discovered this issue (R2018a), along with the failure of uistack() to work-around. I am sincerely hoping that yyaxis() was designed for extensibility and new feature support. I recall that Loren Shure facilitated a very good group discussion of desired functionality.
Hopefully, it won't take too many more release cycles (we're now at 4) before this issue is resolved? I appreciate having yyaxis, though to be honest I am disappointed that it doesn't support multiple Y-axes per side. (Commonly seen in academic and financial contexts.)
Jurgens Wolfaardt
Jurgens Wolfaardt 2018년 9월 17일
If numbers of requests add impetus; please add mine! I have to use yyaxis in appdesigner (plotyy not allowed) and now I'm stuck with noisy plots obscuring everything. I don't only need to have left/right order selection, I need to set individual line order, regardless of what axis it's on.
Daniel Murphy
Daniel Murphy 2018년 9월 19일
+1
Most of my thoughts have already been captured by others above (over 2 years!?), so I'd just like to add my request to the pile - having to use a workaround for this is disappointing.
Joey
Joey 2018년 11월 5일
+1
please enable uistack to allow my yyaxis right curves to be in the background

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

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

질문:

2016년 4월 20일

댓글:

2025년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by