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

6 Ansichten (letzte 30 Tage)
Kristine
Kristine am 20 Feb. 2025
Bearbeitet: Stephen23 am 20 Feb. 2025
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 Kommentar
Kristine
Kristine am 20 Feb. 2025
% 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';

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Feb. 2025
Bearbeitet: Stephen23 am 20 Feb. 2025
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';

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by