Why do my plots overlap instead of showing up side-by-side?

조회 수: 7 (최근 30일)
Kristine
Kristine 2025년 2월 20일
편집: Stephen23 2025년 2월 20일
t = tiledlayout(2,4);
nexttile;
x1 = linspace (0,10); %This is not my actual data
y1 = sin(3*x);
Unrecognized function or variable 'x'.
x2 = linspace (2,30);
y2 = sin(3*x);
ax1 = axes(t);
plot(ax1,x1,y1,'-','lineWidth', 2,'Color',[0.6350 0.0780 0.1840]);
set(gca,'Ydir','reverse');
ylabel('Depth (m)');
xlabel('Temperature (C)');
ax1.XColor = [0.6350 0.0780 0.1840];
ax1.YColor = 'k';
ax2 = axes(t);
plot(ax2,x2,y2,'-','lineWidth', 2,'Color',[0 0.4470 0.7410]);
set(gca,'Ydir','reverse');
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
variableYaxis = ax2.YAxis; % Define the y-axis as a variable
variableYaxis.Visible = 'off'; % Turn off variable visibility
xlabel('Salinity (PSU)');
ax2.XColor = [0 0.4470 0.7410];
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
nexttile
ax3 = axes(t);
plot(ax3,x1,y1,'-','lineWidth', 2,'Color',[0.6350 0.0780 0.1840])
set(gca,'Ydir','reverse');
ylabel('Depth (m)');
xlabel('Temperature (C)');
ax3.XColor = [0.6350 0.0780 0.1840];
ax3.YColor = 'k';
ax4 = axes(t);
plot(ax2,x2,y2,'-','lineWidth', 2,'Color',[0 0.4470 0.7410]);
set(gca,'Ydir','reverse');
ax4.XAxisLocation = 'top';
ax4.YAxisLocation = 'right';
variableYaxis = ax4.YAxis; % Define the y-axis as a variable
variableYaxis.Visible = 'off'; % Turn off variable visibility
xlabel('Salinity (PSU)');
ax4.XColor = [0 0.4470 0.7410];
ax4.Color = 'none';
ax3.Box = 'off';
ax4.Box = 'off';
  댓글 수: 1
Kristine
Kristine 2025년 2월 20일
% I don't know what went wrong with the code above, but this is what I
% wanted to show. The graphs overlap and I don't know how to fix this.
figure (4);
t = tiledlayout(2,4);
nexttile
x1 = [1,2,3,4,5,6,7,8,9,10,11,11,10,9,8,7,6,5,4,3];
y1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
x2 = [2,5,6,7,5,6,7,10,9,10,11,11,10,9,7,7,7,7,7,7];
y2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
ax1 = axes(t);
plot(ax1,x1,y1,'-','lineWidth', 2,'Color',[0.6350 0.0780 0.1840]);
set(gca,'Ydir','reverse');
ylabel('Depth (m)');
xlabel('Temperature (C)');
ax1.XColor = [0.6350 0.0780 0.1840];
ax1.YColor = 'k';
ax2 = axes(t);
plot(ax2,x2,y2,'-','lineWidth', 2,'Color',[0 0.4470 0.7410]);
set(gca,'Ydir','reverse');
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
variableYaxis = ax2.YAxis; % Define the y-axis as a variable
variableYaxis.Visible = 'off'; % Turn off variable visibility
xlabel('Salinity (PSU)');
ax2.XColor = [0 0.4470 0.7410];
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
nexttile
ax1 = axes(t);
plot(ax1,x1,y1,'-','lineWidth', 2,'Color',[0.6350 0.0780 0.1840]);
set(gca,'Ydir','reverse');
ylabel('Depth (m)');
xlabel('Temperature (C)');
ax1.XColor = [0.6350 0.0780 0.1840];
ax1.YColor = 'k';
ax2 = axes(t);
plot(ax2,x2,y2,'-','lineWidth', 2,'Color',[0 0.4470 0.7410]);
set(gca,'Ydir','reverse');
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
variableYaxis = ax2.YAxis; % Define the y-axis as a variable
variableYaxis.Visible = 'off'; % Turn off variable visibility
xlabel('Salinity (PSU)');
ax2.XColor = [0 0.4470 0.7410];
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';

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

채택된 답변

Stephen23
Stephen23 2025년 2월 20일
편집: Stephen23 2025년 2월 20일
Explanation: TILEDLAYOUT was written assuming one axes in each tile position. When you create a 2nd (or more) axes in a specific tile position it resets somehow and goes back to first tile position, so they all end up there. Not very useful if you intentionally want to create multiple axes in one tile position (e.g. for multiple X or Y scales).
However the solution is also quite simple... lets first create some fake data:
x1 = [1,2,3,4,5,6,7,8,9,10,11,11,10,9,8,7,6,5,4,3];
y1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
x2 = [2,5,6,7,5,6,7,10,9,10,11,11,10,9,7,7,7,7,7,7];
y2 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
myb = [0,0.4470,0.7410];
myr = [0.6350,0.0780,0.1840];
Solution: For two or more axes (in one tile position) you will need to manually set those extra axes' LAYOUT.TILE property yourself. I also made improvements / fixed a few other bugs in your code, e.g.
  • replaced GCA with explicit axes handles,
  • removed superfuous AXES calls,
  • simplifed the Y reversal,
  • made explicit references to the axes handle for all labels etc.
tlh = tiledlayout(2,4);
%
ax1 = nexttile(tlh);
plot(ax1,x1,y1,'-','lineWidth', 2,'Color',myr);
ylabel(ax1,'Depth (m)');
xlabel(ax1,'Temperature (C)');
ax1.YDir = 'reverse';
ax1.XColor = myr;
ax1.YColor = 'k';
ax1.Box = 'off';
%
ax2 = axes(tlh); % !!!
ax2.Layout.Tile = ax1.Layout.Tile; % !!!
plot(ax2,x2,y2,'-','lineWidth', 2,'Color',myb);
xlabel(ax2,'Salinity (PSU)');
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.YAxis.Visible = 'off';
ax2.YDir = 'reverse';
ax2.XColor = myb;
ax2.Color = 'none';
ax2.Box = 'off';
%
ax3 = nexttile(tlh);
plot(ax3,x1,y1,'-','lineWidth', 2,'Color',myr)
ylabel(ax3,'Depth (m)');
xlabel(ax3,'Temperature (C)');
ax3.YDir = 'reverse';
ax3.XColor = myr;
ax3.YColor = 'k';
ax3.Box = 'off';
%
ax4 = axes(tlh); % !!!
ax4.Layout.Tile = ax3.Layout.Tile; % !!!
plot(ax4,x2,y2,'-','lineWidth', 2,'Color',myb);
xlabel(ax4,'Salinity (PSU)');
ax4.XAxisLocation = 'top';
ax4.YAxisLocation = 'right';
ax4.YAxis.Visible = 'off';
ax4.YDir = 'reverse';
ax4.XColor = myb;
ax4.Color = 'none';
ax4.Box = 'off';

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by