Draw stack order for yyaxis

156 ビュー (過去 30 日間)
Charles Brown
Charles Brown 2016 年 4 月 20 日
コメント済み: Calvin He 2022 年 12 月 1 日
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 日
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 日
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')
  3 件のコメント
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:

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


Mike Garrity
Mike Garrity 2016 年 4 月 20 日
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 件のコメント
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 日

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

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by