How to plot 4 Tiled plots with 2 x axis and 2 y axis in one figure

82 views (last 30 days)
I want to plot 4 tiled diagrams in one figure. I already use tiledlayout to creat a secon x axis. The first diagram is correct, but when I try to plot the second diagram next to the first (using tiled layout again) only an empty diagram appears and the data from the second diagram are plottet in the first diagram.
How can i plot 4 of those diagramms like the first in one figure?
% Al2O3 für alle EF
% EF 4 mm
tmp = readtable('project.xlsx', 'Sheet', '4');
x1= table2array(tmp(:,12));
y1= table2array(tmp(:,2));
x2= table2array(tmp(:,25))
y2= table2array(tmp(:,15));
y3= table2array(tmp(:,3));
y4= table2array(tmp(:,16));
figure()
t = tiledlayout(2,3);
t1=tiledlayout(t,1,1)
ax1 = axes(t);
plot(ax1,x1,y1,'or')
ax1.XColor = 'r';
ax1.YColor = 'r';
set ( gca,'XLim', [0, 4])
xlabel('Thickness [mm]')
ylabel('w/w [%]')
ax2 = axes(t);
plot(ax2,x2,y2,'ok')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
set ( gca,'XLim', [0, 4], 'XDir', 'reverse' )
set ( gca,'YLim', [0, 0.525])
xlabel('Thickness [mm]')
ylabel('w/w[%]')
nexttile;
t2=tiledlayout(t,1,1)
ax1 = axes(t);
plot(ax1,x1,y2,'or')
ax1.XColor = 'r';
ax1.YColor = 'r';
set ( gca,'XLim', [0, 4])
xlabel('Thickness [mm]')
ylabel('w/w [%]')
ax2 = axes(t);
plot(ax2,x2,y3,'ok')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
set ( gca,'XLim', [0, 4], 'XDir', 'reverse' ) % Hier muss hintere Zahl mit Glas-Dicke (Nenndicke) übereinstimmen
xlabel('Thickness [mm])')
ylabel('w/w [%]')
  3 Comments
Stephen23
Stephen23 on 21 Nov 2024 at 9:14
Rather than creating a sub-table and then calling TABKE2ARRAY like this:
x1 = table2array(tmp(:,12));
a simpler way to obtain the table content is to use curly-brace indexing:
x1 = tmp{:,12};

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 21 Nov 2024 at 9:07
Do NOT call TILEDLAYOUT multiple times.
Only call TILEDLAYOUT once. After that you call NEXTTILE for each plot.
"I want to plot 4 tiled diagrams in one figure."
Your code specifies two rows and three columns, which thus makes space for six plots:
t = tiledlayout(2,3);
whereas if you really want four plots then you should specify two rows and two columns:
tiledlayout(2,2); % call only ONCE!
[X,Y,Z] = peaks(20);
% Tile 1
nexttile
surf(X,Y,Z)
% Tile 2
nexttile
contour(X,Y,Z)
% Tile 3
nexttile
imagesc(Z)
% Tile 4
nexttile
plot3(X,Y,Z)
  11 Comments
Stephen23
Stephen23 on 21 Nov 2024 at 14:36
@Luisa: I hope it helped. Please remember to click the accept buttton!

Sign in to comment.

More Answers (1)

Rahul
Rahul on 21 Nov 2024 at 13:33
Edited: Rahul on 21 Nov 2024 at 13:37
Hi @Luisa,
I believe that you are trying to to plot 4 tiled diagram in a single figure using tiledlayout function, where you are getting empty diagrams apart from the first plot. Moreover, the second plot’s data is being overlapped with the first one.
In the given script, the specified dimensions of tiledlayout function have been set to (2,3) which indicates axes for 6 plots, instead of required 4 plots on a single figure.
Instead, you can try using any any (m, n) dimensions with product as 4, as shown below:
tiledlayout(2,2);
Instead of repeatedly calling tiledlayout function to access the axes property of each tile, you can use nexttile(i) function to perform the same operation for the i(th) tile.
ax1 = nexttile(1)
Moreover, the axes object of tiles 1 and 2 are being used to modify axes properties like styling and data, for other tiles (3 and 4). This leads to overlapping of plot data and its markers, in the same tile. Instead, you can use thenexttile’ function for each numbered tile to access only their corresponding properties.
Here's how you can structure your code for a better plotting behaviour:
% Al2O3 für alle EF
% EF 4 mm
tmp = readtable('project.xlsx', 'Sheet', '4');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
x1= table2array(tmp(:,12));
y1= table2array(tmp(:,2));
x2= table2array(tmp(:,25))
x2 = 36×1
3.7880 3.7200 3.6720 3.6140 3.5950 3.5310 3.4850 3.4430 3.4210 3.3750
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y2= table2array(tmp(:,15));
y3= table2array(tmp(:,3));
y4= table2array(tmp(:,16));
figure()
tiledlayout(2,2);
ax1 = nexttile(1);
plot(ax1,x1,y1,'or')
ax1.XColor = 'r';
ax1.YColor = 'r';
set ( gca,'XLim', [0, 4])
xlabel('Thickness [mm]')
ylabel('w/w [%]')
ax2 = nexttile(2);
plot(ax2,x2,y2,'ok')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax2.Box = 'off';
ax2.Box = 'off';
set ( gca,'XLim', [0, 4], 'XDir', 'reverse' )
set ( gca,'YLim', [0, 0.525])
xlabel('Thickness [mm]')
ylabel('w/w[%]')
ax3 = nexttile(3);
plot(ax3,x1,y2,'or')
ax3.XColor = 'r';
ax3.YColor = 'r';
set ( gca,'XLim', [0, 4])
xlabel('Thickness [mm]')
ylabel('w/w [%]')
nexttile
ax4 = nexttile(4);
plot(ax4,x2,y3,'ok')
ax4.XAxisLocation = 'top';
ax4.YAxisLocation = 'right';
ax4.Color = 'none';
ax4.Box = 'off';
ax4.Box = 'off';
set (gca,'XLim', [0, 4], 'XDir', 'reverse') % Hier muss hintere Zahl mit Glas-Dicke (Nenndicke) übereinstimmen
xlabel('Thickness [mm])')
ylabel('w/w [%]')
To know more about the usage oftiledlayout function, you can refer to the following documentation link:
Cheers!
  1 Comment
Luisa
Luisa on 21 Nov 2024 at 14:15
@RahulYes thtat is exactly what i try to plot. So i combine ax1 and ax2 to get just one diagramm. This works well for the first diagramm but when i try to combine x3 and x4 to a second diagram than the diagramms are empty
One figure is plotted with the following code like this:
tmp = readtable('project.xlsx', 'Sheet','4');
x1= table2array(tmp(:,12));
y1= table2array(tmp(:,2));
x2= table2array(tmp(:,25))
y2= table2array(tmp(:,15));
figure()
t = tiledlayout(2,2);
ax1 = axes(t);
plot(ax1,x1,y1,'or')
ax1.XColor = 'r';
ax1.YColor = 'r';
set ( gca,'XLim', [0, 4])
set ( gca,'YLim', [0.46, 0.56])
xlabel('Thickness [mm]')
ylabel('w/w [%]')
ax2 = axes(t);
plot(ax2,x2,y2,'ok')
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
ax1.Box = 'off';
ax2.Box = 'off';
set ( gca,'XLim', [0, 4], 'XDir', 'reverse' )
set ( gca,'YLim', [0.46, 0.56])
xlabel('Thickness [mm]')
ylabel('w/w [%]')
Now I tried to add a second diagram like this using x1,y3 und x2,y4 as input data.

Sign in to comment.

Categories

Find more on Axes Appearance in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!