How to add a legend for multiple figures with horizontal and south location
4 次查看(过去 30 天)
显示 更早的评论
% 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 个评论
采纳的回答
Star Strider
2024-2-24
编辑:Star Strider
2024-2-24
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!