MATLAB Answers

is there any way to put two plots in one legend entry?

86 views (last 30 days)
is it possible to combine multiple plots in one legend entry as shown in the picture? . I tried various operations with [] and () but it didn't work.
Is there a way to put two plots in one legend entry?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Sep 2019
That happens automatically unless you do one of the following:
  • call legend with a single legend entry, such as inside a loop or trying to place the legend call for each plot() right after the associated plot(); or call legend passing in only one graphics object handle
Some of the approaches that work are:
A)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
thislegend = sprintf('R%d', K); %construct a legend string
legends{K} = thislegend; %record it
plot(this_x, this_y);
hold on
end
legend(legends);
B)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
thislegend = sprintf('R%d', K); %construct a legend string
legends{K} = thislegend; %record it
linehandles(K) = plot(this_x, this_y);
hold on
end
legend(linehandles, legends);
C)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
thislegend = sprintf('R%d', K); %construct a legend string
linehandles(K) = plot(this_x, this_y, 'DisplayName', thislegend);
hold on
end
legend(linehandles, 'show');
D)
for K = 1 : 6
this_x = something(K,:);
this_y = someother(K,:);
plot(this_x, this_y);
hold on
end
legend({'R1', 'R2', 'R3', 'R4', 'R5', 'R6'});

  0 Comments

Sign in to comment.

More Answers (1)

Fuad Nammas
Fuad Nammas on 18 Sep 2019
Edited: Walter Roberson on 19 Sep 2019
thanks Walter Roberson but what i am asking is:
Can I put the same colored curve (please check my pic) with the the same legend.i.e., two black curves with legend R3 and the two blue curves with legend R2, etc....
I appreciate your efforts dear.
also, below you will see my matlab code
aB=9.8*10.^(-9);
q=1.6*10.^(-19);
me=0.067*9.1*10.^(-31);
h=1.0545*10.^(-34);
kb=1.38*10.^(-23);
om1=3*10.^(12);
om2=0;
B=2;
c=q./(2*pi);
R1=aB;
R2=1.2*aB;
R3=1.4*aB;
w01=h./(((me*R1.^2*(sqrt(2*pi)))));
w02=h./(((me*R2.^2*(sqrt(2*pi)))));
w03=h./(((me*R3.^2*(sqrt(2*pi)))));
T=1:1:80;
wc=q*B/me;
y=sinh(0.5.*h.*wc./(kb.*T));
w1=sqrt(w01.^2+0.25*wc.^2);
w2=sqrt(w02.^2+0.25*wc.^2);
w3=sqrt(w03.^2+0.25*wc.^2);
om01=sqrt(w1.^2-om1.^2);
om02=sqrt(w2.^2-om1.^2);
om03=sqrt(w3.^2-om1.^2);
Icm1=c.*w1.*y./sinh(h.*w1./(kb.*T))-0.5.*c.*wc;
Irel1=c.*om01.*y./sinh(h.*om01./(kb.*T))-0.5.*c.*wc;
I1=Icm1+Irel1;
Icm2=c.*w2.*y./sinh(h.*w2./(kb.*T))-0.5.*c.*wc;
Irel2=c.*om02.*y./sinh(h.*om02./(kb.*T))-0.5.*c.*wc;
I2=Icm2+Irel2;
Icm3=c.*w3.*y./sinh(h.*w3./(kb.*T))-0.5.*c.*wc;
Irel3=c.*om03.*y./sinh(h.*om03./(kb.*T))-0.5.*c.*wc;
I3=Icm3+Irel3;
om011=sqrt(w1.^2-om2.^2);
om022=sqrt(w2.^2-om2.^2);
om033=sqrt(w3.^2-om2.^2);
Icm11=c.*w1.*y./sinh(h.*w1./(kb.*T))-0.5.*c.*wc;
Irel11=c.*om011.*y./sinh(h.*om011./(kb.*T))-0.5.*c.*wc;
I11=Icm11+Irel11;
Icm22=c.*w2.*y./sinh(h.*w2./(kb.*T))-0.5.*c.*wc;
Irel22=c.*om022.*y./sinh(h.*om022./(kb.*T))-0.5.*c.*wc;
I22=Icm22+Irel22;
Icm33=c.*w3.*y./sinh(h.*w3./(kb.*T))-0.5.*c.*wc;
Irel33=c.*om033.*y./sinh(h.*om033./(kb.*T))-0.5.*c.*wc;
I33=Icm33+Irel33;
nm=1*10.^(-9);
plot(T,I1/nm,'r',T,I11/nm,'--r',T,I2/nm,'b',T,I22/nm,'--b',T,I3/nm,'k',T,I33/nm,'--k')
legend('R_1=a_B','R_1=a_B','R_2=1.2a_B','R_2=1.2a_B','R_3=1.4a_B','R_3=1.4a_B')
xlabel('T(K)')
ylabel('I (nA)')

  2 Comments

Walter Roberson
Walter Roberson on 19 Sep 2019
h = plot(T,I1/nm,'r',T,I11/nm,'--r',T,I2/nm,'b',T,I22/nm,'--b',T,I3/nm,'k',T,I33/nm,'--k');
legend(h(1:2:end), {'R_1=a_B','R_2=1.2a_B','R_3=1.4a_B'})
Fuad Nammas
Fuad Nammas on 21 Sep 2019
Many many thanks to you dear Walter
you are my hero

Sign in to comment.

Sign in to answer this question.

Tags


Translated by