![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/605375/image.png)
How to insert a unique legend with subplot?
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following plot:
y1 = [2,2,2,2];
y2 = [2.15, 2.115, 2.08, 2.07];
y3 = [0.08532, 0.071189, 0.05391, 0.06123];
y11 = [2,2,2,2];
y22 = [2.19,2.13,2.09,2.07];
y33= [0.1134, 0.1233,0.1122,0.1202];
y111 = [2,2,2,2];
y222 = [2.19,2.15,2.09,2.1];
y333= [0.1834, 0.1433,0.1622,0.1802];
x = [1:4];
h = figure;
subplot(1,3,1)
plot(x,y1,'*-r', 'LineWidth', 1)
hold on
plot(x, y2, '*-k', 'LineWidth',1)
hold on
plot(x, y3, '*-b', 'LineWidth',1)
hold on
plot(4.5, 2.08,'r.', 'MarkerSize', 20)
hold on
plot(4.5, 2.03,'k.', 'MarkerSize', 20)
hold on
plot(4.5, 0.0146331392,'b.', 'MarkerSize', 20)
box on
hold on
xticklabels({'Jan 7','Jan 14','Jan 21','Jan 28'})
xtickangle(45)
%ylim([-0.05 0.15])
title('January 2005')
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get(titleHandle , 'position' );
pos1 = pos + [0 0.005 0]
set(titleHandle , 'position' , pos1);
legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual','Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2)
subplot(1,3,2)
plot(x,y11,'*-r', 'LineWidth', 1)
hold on
plot(x, y22, '*-k', 'LineWidth',1)
hold on
plot(x, y33, '*-b', 'LineWidth',1)
hold on
plot(4.5,2.08,'r.', 'MarkerSize', 20)
hold on
plot(4.5,2.05,'k.', 'MarkerSize', 20)
hold on
plot(4.5,0.0938280876,'b.', 'MarkerSize', 20)
box on
hold on
xticklabels({'April 7','April 14','April 21','April 28'})
xtickangle(45)
title('April 2005')
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get(titleHandle , 'position' );
pos1 = pos + [0 0.005 0]
set(titleHandle , 'position' , pos1);
legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual', 'Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2)
subplot(1,3,3)
plot(x,y111,'*-r', 'LineWidth', 1)
hold on
plot(x, y222, '*-k', 'LineWidth',1)
hold on
plot(x, y333, '*-b', 'LineWidth',1)
hold on
line1 = plot(4.5,2.08,'r.', 'MarkerSize', 20)
hold on
line2 =plot(4.5,1.98,'k.', 'MarkerSize', 20)
hold on
line3 =plot(4.5,0.2170966370,'b.', 'MarkerSize', 20)
box on
hold on
xticklabels({'Aug 7','Aug 14','Aug 21','Aug 28'})
xtickangle(45)
title('August 2005')
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get(titleHandle , 'position' );
pos1 = pos + [0 0.005 0]
set(titleHandle , 'position' , pos1);
legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual', 'Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2)
In the plot, I have 3 legends for each plot which are identical. I would like to have one unique legend below and outside (centred) the plots. I have read suggestions around but I couldn't make it work.
Can anyone help me with this?
Thanks!
0 commentaires
Réponse acceptée
DGM
le 3 Mai 2021
This is kind of roundabout, but maybe it helps:
subplot(1,3,1)
plot(x,y1,'*-r', 'LineWidth', 1)
hold on
plot(x, y2, '*-k', 'LineWidth',1)
hold on
plot(x, y3, '*-b', 'LineWidth',1)
hold on
plot(4.5, 2.08,'r.', 'MarkerSize', 20)
hold on
plot(4.5, 2.03,'k.', 'MarkerSize', 20)
hold on
plot(4.5, 0.0146331392,'b.', 'MarkerSize', 20)
box on
hold on
xticklabels({'Jan 7','Jan 14','Jan 21','Jan 28'})
xtickangle(45)
%ylim([-0.05 0.15])
title('January 2005')
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get(titleHandle , 'position' );
pos1 = pos + [0 0.005 0]
set(titleHandle , 'position' , pos1);
% when the legend is placed, it automatically resizes gca to a size we want
lh=legend('EONIA', 'EU Shadow Rate', 'Textual', 'Actual EONIA', 'Actual Shadow', 'Actual Textual','Orientation', 'horizontal', 'Location', 'southoutside', 'NumColumns',2);
% store this size so we can use it later so that all axes leave room
axpos1 = get(gca,'outerposition');
% moving the legend changes the size of gca again
lh.Position(1) = 0.5-lh.Position(3)/2;
set(gca,'outerposition',axpos1) % so change it back
subplot(1,3,2)
plot(x,y11,'*-r', 'LineWidth', 1)
hold on
plot(x, y22, '*-k', 'LineWidth',1)
hold on
plot(x, y33, '*-b', 'LineWidth',1)
hold on
plot(4.5,2.08,'r.', 'MarkerSize', 20)
hold on
plot(4.5,2.05,'k.', 'MarkerSize', 20)
hold on
plot(4.5,0.0938280876,'b.', 'MarkerSize', 20)
box on
hold on
xticklabels({'April 7','April 14','April 21','April 28'})
xtickangle(45)
title('April 2005')
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get(titleHandle , 'position' );
pos1 = pos + [0 0.005 0]
set(titleHandle , 'position' , pos1);
% change this axes to make room for legend
axpos2 = get(gca,'outerposition');
set(gca,'outerposition',[axpos2(1) axpos1(2) axpos2(3) axpos1(4)])
subplot(1,3,3)
plot(x,y111,'*-r', 'LineWidth', 1)
hold on
plot(x, y222, '*-k', 'LineWidth',1)
hold on
plot(x, y333, '*-b', 'LineWidth',1)
hold on
line1 = plot(4.5,2.08,'r.', 'MarkerSize', 20)
hold on
line2 =plot(4.5,1.98,'k.', 'MarkerSize', 20)
hold on
line3 =plot(4.5,0.2170966370,'b.', 'MarkerSize', 20)
box on
hold on
xticklabels({'Aug 7','Aug 14','Aug 21','Aug 28'})
xtickangle(45)
title('August 2005')
set(gca,'Units','normalized')
titleHandle = get( gca ,'Title' );
pos = get(titleHandle , 'position' );
pos1 = pos + [0 0.005 0]
set(titleHandle , 'position' , pos1);
% change this axes to make room for legend
axpos3 = get(gca,'outerposition');
set(gca,'outerposition',[axpos3(1) axpos1(2) axpos3(3) axpos1(4)])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/605375/image.png)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!