Draw stack order for yyaxis
古いコメントを表示
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.
採用された回答
その他の回答 (2 件)
Kouichi C. Nakamura
2021 年 4 月 23 日
As DJ pointed out, the link Plot line-chart on the left axis uper the bar-chart on the right axis, is very useful.
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
2021 年 9 月 13 日
Thanks!
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
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
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
2016 年 4 月 20 日
Charles Brown
2016 年 4 月 20 日
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
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
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
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
2018 年 6 月 28 日
Is the problem with yyaxis and uistack to be fixed?
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
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
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
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
2018 年 11 月 5 日
+1
please enable uistack to allow my yyaxis right curves to be in the background
DJ
2018 年 11 月 18 日
See this answer here for a easy solution: https://stackoverflow.com/questions/37709988/plot-line-chart-on-the-left-axis-uper-the-bar-chart-on-the-right-axis
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!