Effacer les filtres
Effacer les filtres

How to add a legend for multiple figures with horizontal and south location

2 vues (au cours des 30 derniers jours)
% Please how can add one legends for the subplots. I want the legend
% location to south and horizontal. I also want to break the legend into
% two lines.
% load
mydata = load('data.mat')
mydata = struct with fields:
nt: 101 paras: [1×1 struct] t: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 … ] (1×101 double) thetazt: [101×201×2 double] vzt: [101×201×2 double] xivals: [-0.2000 0.2000] z: [0 1.0000e-06 2.0000e-06 3.0000e-06 4.0000e-06 5.0000e-06 6.0000e-06 7.0000e-06 8.0000e-06 9.0000e-06 1.0000e-05 1.1000e-05 1.2000e-05 1.3000e-05 1.4000e-05 … ] (1×201 double) zm: 101
xivals = mydata.xivals
xivals = 1×2
-0.2000 0.2000
pars = mydata.paras;
% u value
u = pars.u;
z = mydata.z;
t = mydata.t;
nt = mydata.nt;
% theta and v
thetazt = mydata.thetazt;
vzt = mydata.vzt;
%% Plot
fig1 = figure(1);
T = tiledlayout(2,2, 'TileSpacing', 'compact');
tinds=[2, 4, 6, 11, 16, 21, 26];
nit=length(tinds);
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p1 = plot(z, thetazt(tinds(it), :, 1), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1);
end
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p2 = plot(z, thetazt(tinds(it), :, 2), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p3 = plot(z, vzt(tinds(it), :, 1), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1 );
end
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p4 = plot(z, vzt(tinds(it), :, 2), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1);
end
% Create a Legend with the data from multiple axes
lg = legend([p1,p2,p3,p4]);
lg.Location = 'south';
lg.Orientation='horizontal';

Réponse acceptée

Star Strider
Star Strider le 24 Fév 2024
Modifié(e) : Star Strider le 24 Fév 2024
I am not certain what you want.
Try this —
% Please how can add one legends for the subplots. I want the legend
% location to south and horizontal. I also want to break the legend into
% two lines.
% load
mydata = load('data.mat')
mydata = struct with fields:
nt: 101 paras: [1x1 struct] t: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ... ] (1x101 double) thetazt: [101x201x2 double] vzt: [101x201x2 double] xivals: [-0.2000 0.2000] z: [0 1.0000e-06 2.0000e-06 3.0000e-06 4.0000e-06 5.0000e-06 6.0000e-06 7.0000e-06 8.0000e-06 9.0000e-06 1.0000e-05 1.1000e-05 1.2000e-05 1.3000e-05 ... ] (1x201 double) zm: 101
xivals = mydata.xivals
xivals = 1x2
-0.2000 0.2000
pars = mydata.paras;
% u value
u = pars.u;
z = mydata.z;
t = mydata.t;
nt = mydata.nt;
% theta and v
thetazt = mydata.thetazt;
vzt = mydata.vzt;
%% Plot
fig1 = figure(1);
T = tiledlayout(4,2, 'TileSpacing', 'compact'); % Change To Specify 3 Rows
tinds=[2, 4, 6, 11, 16, 21, 26];
nit=length(tinds);
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p1(it) = plot(z, thetazt(tinds(it), :, 1), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1);
end
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p2(it) = plot(z, thetazt(tinds(it), :, 2), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p3(it) = plot(z, vzt(tinds(it), :, 1), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1 );
end
nexttile(T)
hold on
for it=1:length(tinds)
xi_part = "\xi = " + xivals(1);
t_part = ", t = " + t(tinds(it));
dn1 = xi_part + t_part;
p4(it) = plot(z, vzt(tinds(it), :, 2), 'Color',[1-(it-1)/(nit-1),(it-1)/(nit-1),1], 'DisplayName', dn1);
end
lt = nexttile(5,[2 2]); % Use Row 3 For Legend Location
% get(lt)
% Create a Legend with the data from multiple axes
lg = legend([p1,p2,p3,p4]);
lg.Location = 'south';
lg.Orientation='vertical';
lg.Position = lt.Position;
lg.NumColumns = 2;
lt.Visible = 'off';
EDIT — (24 Feb 2024 at 17:25)
Subscripted ‘p1’ ... ‘p4’ so that they now all appear in the legend, as requested in the Comment.
.
  4 commentaires
University
University le 24 Fév 2024
Thank you for your explanation
Star Strider
Star Strider le 24 Fév 2024
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by