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

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 commentaires

Rahul
Rahul le 21 Nov 2024
Modifié(e) : Rahul le 21 Nov 2024
Hi @Luisa,
Can you share the 'project.xlsx' file, that'll help in reproducing the issue.
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};

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

Thanks for the hint. I have a problem with plotting the second axis (y and x) after calling nexttile. How can i plot the second axis correct?
Tidying up that workbook would make importing the data simpler and more reliable, e.g.:
  • fill out the table starting from cell A1,
  • no empty columns,
  • no duplicate header names.
T = readtable('project.xlsx', 'Sheet','4', 'Range','L2:P18')
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.
T = 16x5 table
Thickness Var2 Steps w_w w_w_1 _________ ____ _____ _____ _____ 3.787 NaN 0 0.524 8.672 3.723 NaN 1 0.518 8.83 3.655 NaN 2 0.512 8.836 3.626 NaN 3 0.513 8.838 3.606 NaN 4 0.514 8.839 3.522 NaN 5 0.512 8.843 3.493 NaN 6 0.517 8.841 3.466 NaN 7 0.515 8.822 3.426 NaN 8 0.515 8.83 3.399 NaN 9 0.513 8.816 3.374 NaN 10 0.512 8.838 3.233 NaN 11 0.513 8.833 3.117 NaN 12 0.517 8.829 3.045 NaN 13 0.514 8.838 2.896 NaN 14 0.513 8.791 2.755 NaN 15 0.512 8.833
tiledlayout(2,2); % call only ONCE!
nexttile
plot(T.Thickness, T.w_w)
nexttile
plot(T.Thickness, T.w_w_1)
nexttile
plot(T.w_w, T.w_w_1)
Thanks so far.
Here is a more cleare tabe (I hope so). I want to plot conc. A_side1 (y axis one) over Thicknes 1 (x Axis one) and in the same diagramm conc.A_side 2 (y axis two) over Thickness 2. Then the nexttile should start the secon plot doing the same with Conc.B side 1 over Thickness 1 and conc.B_side 2 over Thickness 2 (again one diagram)
Note the lower case with CONC_A_SIDE1, consistent naming would make data easier to work with.
T = readtable('project.xlsx')
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.
T = 16x25 table
Steps conc_A_side1 Conc_B_Side1 Thickness1 Conc_A_Side2 Conc_B_Side2 Thickness2 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Thickness _____ ____________ ____________ __________ ____________ ____________ __________ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _________ 0 0.495 8.518 3.787 0.524 8.672 3.788 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.788 1 0.519 8.83 3.723 0.518 8.83 3.72 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.72 2 0.515 8.834 3.655 0.512 8.836 3.672 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.672 3 0.515 8.821 3.626 0.513 8.838 3.614 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.614 4 0.515 8.84 3.606 0.514 8.839 3.595 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.595 5 0.517 8.829 3.522 0.512 8.843 3.531 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.531 6 0.513 8.838 3.493 0.517 8.841 3.485 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.485 7 0.512 8.837 3.466 0.515 8.822 3.443 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.443 8 0.514 8.838 3.426 0.515 8.83 3.421 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.421 9 0.517 8.83 3.399 0.513 8.816 3.375 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.375 10 0.517 8.835 3.374 0.512 8.838 3.35 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.35 11 0.515 8.827 3.233 0.513 8.833 3.197 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.197 12 0.515 8.837 3.117 0.517 8.829 3.09 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.09 13 0.513 8.83 3.045 0.514 8.838 3.018 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.018 14 0.514 8.822 2.896 0.513 8.791 2.85 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.85 15 0.514 8.828 2.755 0.512 8.833 2.715 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.715
tiledlayout('flow') % call only ONCE!
nexttile
plot(T.Thickness1,T.conc_A_side1, T.Thickness2,T.Conc_A_Side2)
nexttile
plot(T.Thickness1,T.Conc_B_Side1, T.Thickness2,T.Conc_B_Side2)
lgh = legend({'Side 1','Side 2'});
lgh.Layout.Tile = 'east';
Thanks for your help, that looks quit good. Now only one more problem exist. T.Thickness2 should have an extra x-Axis because the x-values frome Side 2 (t.Thickness2) must be revers to those of T.Thickness1. Is there any possibility to creat this in the diagramm? (x.axis bottom form 2.6-3.8 for T.Thickness1 and x.axis top from 3.8 to 2.6 for T.Thickness2)
From this page i used the side i get the code to plot to x-Axis. For one figure it is working. But if i want to plot a second figure with multiple x-axis in the same figure it is not working
tbl = readtable('project.xlsx')
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.
tbl = 16x25 table
Steps conc_A_side1 Conc_B_Side1 Thickness1 Conc_A_Side2 Conc_B_Side2 Thickness2 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Thickness _____ ____________ ____________ __________ ____________ ____________ __________ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _________ 0 0.495 8.518 3.787 0.524 8.672 3.788 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.788 1 0.519 8.83 3.723 0.518 8.83 3.72 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.72 2 0.515 8.834 3.655 0.512 8.836 3.672 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.672 3 0.515 8.821 3.626 0.513 8.838 3.614 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.614 4 0.515 8.84 3.606 0.514 8.839 3.595 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.595 5 0.517 8.829 3.522 0.512 8.843 3.531 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.531 6 0.513 8.838 3.493 0.517 8.841 3.485 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.485 7 0.512 8.837 3.466 0.515 8.822 3.443 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.443 8 0.514 8.838 3.426 0.515 8.83 3.421 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.421 9 0.517 8.83 3.399 0.513 8.816 3.375 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.375 10 0.517 8.835 3.374 0.512 8.838 3.35 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.35 11 0.515 8.827 3.233 0.513 8.833 3.197 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.197 12 0.515 8.837 3.117 0.517 8.829 3.09 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.09 13 0.513 8.83 3.045 0.514 8.838 3.018 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3.018 14 0.514 8.822 2.896 0.513 8.791 2.85 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.85 15 0.514 8.828 2.755 0.512 8.833 2.715 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.715
tlh = tiledlayout(2,1); % call only ONCE!
ax1 = nexttile(tlh);
ax2 = axes(tlh);
ax2.Layout.Tile = 1;
plot(ax1, tbl.Thickness1,tbl.conc_A_side1, '-r')
plot(ax2, tbl.Thickness2,tbl.Conc_A_Side2, '-k')
ax2.Color = 'none';
ax2.XDir = 'reverse';
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax1.Box = 'off';
ax2.Box = 'off';
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1 = nexttile(tlh);
ax2 = axes(tlh);
ax2.Layout.Tile = 2;
plot(ax1, tbl.Thickness1,tbl.Conc_B_Side1, '-r')
plot(ax2, tbl.Thickness2,tbl.Conc_B_Side2, '-k')
ax2.Color = 'none';
ax2.XDir = 'reverse';
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax1.Box = 'off';
ax2.Box = 'off';
ax1.XColor = 'r';
ax1.YColor = 'r';
Note that the horizontal alignment between the X-axes is arbitrary and has no significance. Set the axes X-limits if you want this to alignment to correspond to some physical parameters/limits.
You can also fiddle around with showing the Y-axes, e.g. show only one Y-axes on the left. For that to make sense you would also need to set the axes Y-limits.
Perfect! Thank you so much!
@Luisa: I hope it helped. Please remember to click the accept buttton!
That was very helpfull !

Connectez-vous pour commenter.

Plus de réponses (1)

Rahul
Rahul le 21 Nov 2024
Modifié(e) : Rahul le 21 Nov 2024
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 commentaire

@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.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by