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 件のコメント

Charles Brown
Charles Brown 2016 年 4 月 20 日
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.
Jacob Lynch August
Jacob Lynch August 2016 年 10 月 31 日
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
DJ
DJ 2018 年 11 月 18 日

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by