How to add a legend for multiple figures with horizontal and south location
조회 수: 6 (최근 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!