How to create 2 legends in one figure?

Hello, I have created two legends in one figure but I dont know how to set the second legend also outside the figure like the another one (screenshot). I think the solution is in this line (I found it in another community) but I dont no how to change it: a=axes('position',get(gca,'position'),'visible','off'); Thanks!
figure(1)
d1=plot(daten.bst.t_Step(1200:2405,1)*3600,daten.bst.T_01(1200:2405,1),'b');
hold on
t1=plot(daten.bst.t_Step(1200:2405,1)*3600,daten.bst.T_01(1200:2405,1),'b');
t2=plot(daten.bst.t_Step(1200:2405,1)*3600,daten.bst.T_02(1200:2405,1)+0.1071,'r');
t3=plot(daten.bst.t_Step(1200:2405,1)*3600,daten.bst.T_03(1200:2405,1)+0.0085,'m');
d2=plot((daten2.bst.t_Step(394:1599,1))*3600,daten2.bst.T_01(394:1599,1),'b--');
plot((daten2.bst.t_Step(394:1599,1)-0.006944)*3600,daten2.bst.T_02(394:1599,1)+0.183,'r--');
plot((daten2.bst.t_Step(394:1599,1)-0.006944)*3600,daten2.bst.T_03(394:1599,1)+0.0708,'m--');
hold off
set(gca);
xlabel('Zeit in s');
ylabel('Temperatur in °C');
xlim([0 1200])
grid on
legend([d1 d2],'1,5 Nm','2 Nm','Location','NorthEastOutside');
a=axes('position',get(gca,'position'),'visible','off');
legend(a,[t1 t2 t3],'T01','T02','T03','Location','EastOutside');

8 commentaires

Walter Roberson
Walter Roberson le 3 Nov 2017
That looks like it could work. Just change EastOutside to East to have it inside the plot.
dpb
dpb le 4 Nov 2017
I didn't try to build an example but the above looks peculiar -- the code says 'Location','EastOutside' for the second but is inside the visible axes based on the second axes position which should be the same as the first.
Walter Roberson
Walter Roberson le 4 Nov 2017
I thought the image was what was desired, and the code is what is available to approximate it?
Ince
Ince le 4 Nov 2017
I want the second legend to be outside the plot. It did not work although the above code says "EastOutside". How should I change this code to have the marked legend outside?
Walter Roberson
Walter Roberson le 4 Nov 2017
I do not have your data to test the code with. When I experiment in R2017b the legends appear where you would expect. Which version are you using?
dpb
dpb le 4 Nov 2017
Modifié(e) : dpb le 5 Nov 2017
With R2014b here I see similar issue as does OP -- the problem is the axes position (width, specifically) of the first axes is reduced when the legend is drawn so when the second is created it isn't as wide as the original was before it was reduced to make room for the legend outside the axes. Then, when the legend is added later, that second axis width is reduced yet again.
Either
  1. Create the second axes before the legend is added to the first, or
  2. set(hA2,'position',get(hA1,'position'))
again afterwards.
Ince
Ince le 4 Nov 2017
dpd, I followed your first point and it worked! Thank youu
dpb
dpb le 5 Nov 2017
Glad to help...sequence is important sometimes...

Connectez-vous pour commenter.

Réponses (0)

Question posée :

le 3 Nov 2017

Commenté :

dpb
le 5 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by