two legends in the same box

4 vues (au cours des 30 derniers jours)
Babu Sankhi
Babu Sankhi le 6 Avr 2021
Modifié(e) : Adam Danz le 7 Avr 2021
Hello,
I want the legends which belong to two y axes in the same box but it only displays one set of legend belonging to only one (red) y axes . Can you help me more ? I want blue ones on the right.
thank you
fig=figure(15);
left_color = [1 0 0];
right_color = [0 0 1];
set(fig,'defaultAxesColorOrder',[left_color; right_color]);
yyaxis left
plot (Ms(1:4,3),Ms(1:4,4)*1000,'ro');hold on
plot (Ms(5,3),Ms(5,4)*1000,'r*')
plot (Ms(6,3),Ms(6,4)*1000,'rs')
plot (Ms(7:8,3),Ms(7:8,4)*1000,'rd')
legend('2 s','2 io','2o','2lo')
xlabel('An (hr)')
ylabel('M (A)')
yyaxis 'right'
plot (Ms(1:4,3),Ms(1:4,5)/1000,'bo')
plot (Ms(5,3),Ms(5,5)/1000,'b*')
plot (Ms(6,3),Ms(6,5)/1000,'bs')
plot (Ms(7:8,3),Ms(7:8,5)/1000,'bd');hold off
legend('2 s','2 io','2o','2lo')
ylabel('µ(T)')
xlim([0.5 4.5])
set(gca,'FontSize',18)
set(gcf,'Position',[100 100 600 500])

Réponse acceptée

Adam Danz
Adam Danz le 6 Avr 2021
Ms = randi(10,10,10);
fig=figure(15);
left_color = [1 0 0];
right_color = [0 0 1];
set(fig,'defaultAxesColorOrder',[left_color; right_color]);
yyaxis left
plot (Ms(1:4,3),Ms(1:4,4)*1000,'ro','DisplayName', '2 s');
hold on
plot (Ms(5,3),Ms(5,4)*1000,'r*', 'DisplayName', '2 io')
plot (Ms(6,3),Ms(6,4)*1000,'rs', 'DisplayName', '2o')
plot (Ms(7:8,3),Ms(7:8,4)*1000,'rd', 'DisplayName', '2lo')
% legend('2 s','2 io','2o','2lo')
xlabel('An (hr)')
ylabel('M (A)')
yyaxis 'right'
plot (Ms(1:4,3),Ms(1:4,5)/1000,'bo','DisplayName', '2 s');
plot (Ms(5,3),Ms(5,5)/1000,'b*', 'DisplayName', '2 io')
plot (Ms(6,3),Ms(6,5)/1000,'bs', 'DisplayName', '2o')
plot (Ms(7:8,3),Ms(7:8,5)/1000,'bd', 'DisplayName', '2lo');
hold off
% legend('2 s','2 io','2o','2lo')
ylabel('µ(T)')
xlim([0.5 4.5])
set(gca,'FontSize',18)
set(gcf,'Position',[100 100 600 500])
legend('NumColumns',2)
  5 commentaires
Babu Sankhi
Babu Sankhi le 7 Avr 2021
Modifié(e) : Babu Sankhi le 7 Avr 2021
Thanks
Adam Danz
Adam Danz le 7 Avr 2021
Modifié(e) : Adam Danz le 7 Avr 2021
You need to change the fontsize of the legend but this will affect the fontsize of all strings in the legend.
legend(___,'FontSize', __)
I don't know whether it's possible to specify the fontsize of single legend strings. There certainly is not a documented approach for that. When it comes to this level of detail, I usually assign labels in programs outside of Matlab (ie, powerpoint).

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by