2 yaxis in a tiledlayout

조회 수: 38 (최근 30일)
Akshayaa Pandiyan
Akshayaa Pandiyan 2021년 5월 26일
편집: Adam Danz 2022년 3월 4일
Hello,
I am using tiledlayout to plot the voltages over 3 range of distance (9-10 cm, 19-20cm, 29-30cm), and would like to add a right y axis to plot the corresponding forces in the same ranges. I have acheived to do this, however, I am struggling when linking the axes. linkaxes() is only able to link the right y axes. I would like link all the left axes together and the right axes together. Is it possible to achieve this?
This is my code:
t = tiledlayout(1,3,'TileSpacing','compact');
bgAx = axes(t,'XTick',[],'YTick',[],'Box','off');
bgAx.Layout.TileSpan = [1 3];
%10cm plot
ax1 = axes(t);
yyaxis left
plot(ax1,x,y1,'LineWidth', 2); hold on; %plotting in left y
xline(ax1,10.5,':');
yyaxis right
plot(ax1,x,y2,'LineWidth', 2); hold on; %plotting in right y
ax1.Box = 'off';
ax1.YAxis(2).Visible = 'off';
xlim(ax1,[9.5 10.5])
grid on; grid minor;
%20cm plot
ax2 = axes(t);
ax2.Layout.Tile = 2;
yyaxis left
plot(ax2,x,y1,'LineWidth', 2);hold on;
xline(ax2,19.5,':');
ax2.YAxis(1).Visible = 'off';
yyaxis right
plot(ax2,x,y2,'LineWidth', 2); hold on;
ax2.YAxis(2).Visible = 'off';
ax2.Box = 'off';
xlim(ax2,[19.5 20.5]);
xline(ax2,20.5,':');
xlabel(ax2,'Distance [cm]', 'FontSize', 12)
grid on; grid minor;
%30cm plot
ax3 = axes(t);
ax3.Layout.Tile = 3;
yyaxis left
plot(ax3,x,y1,'LineWidth', 2);hold on;
yyaxis right
ylabel("Pressure [N]");
plot(ax3,x,y2,'LineWidth', 2); hold on;
xline(ax3,29.5,':');
ax3.YAxis(1).Visible = 'off';
ax3.Box = 'off';
xlim(ax3,[29.5 30.5]);
% Link the axes
linkaxes([ax1 ax2 ax3], 'y') %-> link only the right y axis
grid on; grid minor;
Thanks,
Akki.

답변 (1개)

Adam Danz
Adam Danz 2021년 5월 26일
편집: Adam Danz 2022년 3월 4일
Check out the examples from the documentation. When using Tiledlayout, axes should either be created using nexttile or after you assign the axes, set the Tile location within the Layout property.
t = tiledlayout(1,3,'TileSpacing','compact');
ax1 = nexttile;
yyaxis left
...
% or
t = tiledlayout(1,3,'TileSpacing','compact');
ax1 = axes(t);
ax1.Layout.Tile = 1;
yyaxis left
...
To link the right y-axes of all 3 tiles, use linkprop
tlo = tiledlayout(1,3);
ax1 = nexttile;
yyaxis right
ax2 = nexttile;
yyaxis right
ax3 = nexttile;
yyaxis right
linkprop([ax1.YAxis(2), ax2.YAxis(2), ax3.YAxis(2)],'Limits');
  댓글 수: 2
Akshayaa Pandiyan
Akshayaa Pandiyan 2021년 5월 26일
I see the problem. But, should I assign limits for the y axis to use the 'limits' linkprop?
Adam Danz
Adam Danz 2021년 5월 26일
Are you asking about setting ylim? The last line in my answer links the y-limits for all 3 axes. If ylim is set prior to calling linkprop, then when when linkprop is set, all 3 right-y-axes will have the same limits as the first axes listed in input #1 in linkprop. If ylim changes for any of the axes after setting linkprop then the ylim will change for all right-y-axes.

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

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by