How do I switch to secondary axes in a tiled layout?
18 views (last 30 days)
Show older comments
I have a tiled layout with two plots, both of which have a secondary set of axes. However, I cannot figure out how to set the secondary axes to be current in the second tile. After calling nexttile, the primary axes plot to tile 2 as expected but the secondary axes plot to tile 1. Full code is attached.
%Plot mu vs alt
ax2 = axes(t) ;% WHAT DO I PUT HERE, THIS JUST STICKS IT BACK IN TILE 1?
plot(ax2,alt,mu,'-k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
xlabel('Dyn. Viscosity, \mu [psf-s]');
atmosphere
0 Comments
Answers (1)
Cris LaPierre
on 7 Dec 2022
You cannot set position of axes in tiledlayout, so I would recommend setting the Layout,Tile property of the fourth axes after it has been created. I'd also avoid reusing the same variable name to capture your axes. Also, use nexttile when appropriate.
Here's an example
%% Plot ISA Troposhere
format long
%% US Customary Units
% Atmosphere
alt=0:36089/100:36089; %BG
p=2116.2166237*(1-alt./145442.16).^5.2558797; %psf
T=59-3.562e-3*alt; %degF
rho=p./(1716.372*(T+459.67))*32.17; %lbm/ft3
Tc=(T-32)*5/9;%degC (for mu).
mu=1.458e-6*sqrt(Tc+273.15)./(1+110.4./(Tc+273.15))*0.020885; %pa-s to psf-s
% Plot p vs alt
figure1=figure;
figure1.Position=[500 200 600 700];
t = tiledlayout(figure1,1,2);
ax1 = nexttile;
plot(ax1,p,alt,'color',[0,0.45,0.74])
ax1.XColor = [0,0.45,0.74];
ax1.YColor = [0,0.45,0.74];
xlabel('Pressure, p [psf]');
ylabel('Altitude, h [ft]');
ax1.YLim=[0,36089];
ax1.GridColor=[0.1,0.1,0.1];
y1ticks=ax1.YTick;
for i=1:numel(y1ticks)
y1tickfixed{i}=num2str(y1ticks(i),'%.0f');
end
set(ax1,'XGrid','on','XMinorGrid','on','YGrid','on','YMinorGrid','on');
ax1.YTickLabel=y1tickfixed;
% Plot T vs alt
ax2 = axes(t);
plot(ax2,T,alt,'-k')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
xlabel('Temperature, T [^{o}F]');
ax2.YLim=[0,36089];
ax2.GridColor=[0.1,0.1,0.1];
y2ticks=ax2.YTick;
for i=1:numel(y2ticks)
y2tickfixed{i}=num2str(y2ticks(i),'%.0f');
end
ax2.YTickLabel=y2tickfixed;
set(ax1,'XGrid','on','XMinorGrid','on','YGrid','on','YMinorGrid','on');
% Plot rho vs alt
ax3 = nexttile;
plot(ax3,rho,alt,'color',[0,0.45,0.74])
ax3.XColor = [0,0.45,0.74];
ax3.YColor = [0,0.45,0.74];
xlabel('Density, \rho [lbm/ft^3]');
ylabel('Altitude, h [ft]');
ax3.YLim=[0,36089];
ax3.GridColor=[0.1,0.1,0.1];
y1ticks=ax3.YTick;
for i=1:numel(y1ticks)
y1tickfixed{i}=num2str(y1ticks(i),'%.0f');
end
set(ax3,'XGrid','on','XMinorGrid','on','YGrid','on','YMinorGrid','on');
ax3.YTickLabel=y1tickfixed;
%Plot mu vs alt
ax4 = axes(t) ;% WHAT DO I PUT HERE, THIS JUST STICKS IT BACK IN TILE 1?
ax4.Layout.Tile = 2;
plot(ax4,alt,mu,'-k')
ax4.XAxisLocation = 'top';
ax4.YAxisLocation = 'right';
ax4.Color = 'none';
ax3.Box = 'off';
ax4.Box = 'off';
xlabel('Dyn. Viscosity, \mu [psf-s]');
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!