How to add a legend for multiple figures with horizontal and south location
4 views (last 30 days)
Show older comments
% 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')
xivals = mydata.xivals
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';
0 Comments
Accepted Answer
Star Strider
on 24 Feb 2024
Edited: Star Strider
on 24 Feb 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')
xivals = mydata.xivals
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)
.
4 Comments
More Answers (0)
See Also
Categories
Find more on Legend 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!