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!