web-dev-qa-db-fra.com

Comment avoir une légende commune pour les sous-parcelles?

J'essaie de créer une figure de sous-intrigues. Je ne veux pas que les sous-intrigues aient des légendes mais plutôt que la figure ait une légende globale.

J'ai lu qu'il est possible soit en ajoutant une légende uniquement au dernier sous-tracé et en ajustant son emplacement dans la figure via la fonction position de legend ou en utilisant une position de sous-tracé (par exemple subplot(2,3,5.5) uniquement pour afficher la légende). Je préférerais la deuxième option même si je n'y suis pas parvenue jusqu'à présent. De l'aide?

Voici mon code:

SLS=figure();
hold on
subplot(3,2,1);
plot(t,u{1},t,u{2},t,u{3},t,u{4},t,u{5},t,u{6});
title('SLS Levels');
subplot(3,2,2);
plot(t,log_u{1},t,log_u{2},t,log_u{3},t,log_u{4},t,log_u{5},t,log_u{6});
title('SLS Logarithms');
subplot(3,2,3);
plot(t,I_u{1},t,I_u{2},t,I_u{3},t,I_u{4},t,I_u{5},t,I_u{6});
title('SLS Levels with Intercept');
subplot(3,2,4);
plot(t,log_I_u{1},t,log_I_u{2},t,log_I_u{3},t,log_I_u{4},t,log_I_u{5},t,log_I_u{6});
title('SLS Log. with Intercept');
subplot(3,2,5.5);
legend('GDP', 'C', 'I', 'G', 'Imp.', 'Exp.');
axis off
10
Whitebeard13

Code:

% Plotting some random data and storing their handles
subplot(3,2,1);       h1 = plot(randperm(10),randperm(10),'ko-');
subplot(3,2,2);       h2 = plot(randperm(10),randperm(10),'g+-');
subplot(3,2,3);       h3 = plot(randperm(10),randperm(10),'md-');
subplot(3,2,4);       h4 = plot(randperm(10),randperm(10),'rv-.');

hL = subplot(3,2,5.5);
poshL = get(hL,'position');     % Getting its position

lgd = legend(hL,[h1;h2;h3;h4],'RandomPlot1','RandomPlot2','RandomPlot3','RandomPlot4');
set(lgd,'position',poshL);      % Adjusting legend's position
axis(hL,'off');                 % Turning its axis off

Sortie:

enter image description here

28
Sardar Usama