How to plot 4 Tiled plots with 2 x axis and 2 y axis in one figure
82 views (last 30 days)
Show older comments
Luisa
on 21 Nov 2024 at 8:55
Commented: Luisa
on 25 Nov 2024 at 5:55
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
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};
Accepted Answer
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
More Answers (1)
Rahul
on 21 Nov 2024 at 13:33
Edited: Rahul
on 21 Nov 2024 at 13:37
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 the ‘nexttile’ 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');
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()
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 of ‘tiledlayout’ function, you can refer to the following documentation link:
Cheers!
See Also
Categories
Find more on Axes Appearance 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!