Draw stack order for yyaxis
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (2)
Kouichi C. Nakamura
il 23 Apr 2021
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 Commenti
舞穹 赵
il 13 Set 2021
Wow, this answer is perfect and saves me a lot of efforts!
Kouichi C. Nakamura
il 13 Set 2021
Thanks!
Calvin He
il 1 Dic 2022
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
il 18 Mar 2025
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
il 20 Apr 2016
1 voto
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 Commenti
Charles Brown
il 20 Apr 2016
Charles Brown
il 20 Apr 2016
Jacob Lynch August
il 31 Ott 2016
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
il 28 Mag 2017
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
il 6 Feb 2018
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
il 28 Feb 2018
Modificato: Christopher Hoen
il 28 Feb 2018
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
il 28 Giu 2018
Is the problem with yyaxis and uistack to be fixed?
Jake Tuttle
il 18 Lug 2018
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
il 1 Ago 2018
+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
il 17 Set 2018
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
il 19 Set 2018
+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
il 5 Nov 2018
+1
please enable uistack to allow my yyaxis right curves to be in the background
DJ
il 18 Nov 2018
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
Categorie
Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!