How to create a legend with multiple plots?

67 views (last 30 days)
andrew
andrew on 7 Mar 2014
Answered: Sahin Ahmed on 14 Apr 2020
The following code is what I used to plot my graphs
h1=plot(exp(Xs),exp(Yest),'-b','LineWidth',2);
hold on
h2=plot(exp(Xs),exp(YCI95s),'--r','LineWidth',2);
h3=plot(exp(Xs),exp(YCI90s),':c','LineWidth',2);
h4=plot(exp(Xs),exp(YCI75s),'--g','LineWidth',2);
h5=plot(exp(Xs),exp(YCI50s),':m','LineWidth',2);
h6= scatter(exp(data1.logAge),exp(data1.logRate),'x','k');
I was wondering if there is a way to make one legend for each plot. h2, h3, h4,h5, are confidence interval bands.

Answers (2)

Doug Hull
Doug Hull on 7 Mar 2014
h(1) = plot(1:5, rand(1,5)+1)
hold on
h(2) = plot(1:5, rand(1,5)+2)
legend('alpha','bravo')
This is one legend for the different lines. I think it is not clear what you want. Is this it?
  1 Comment
andrew
andrew on 7 Mar 2014
This is my plot I have 2 lines with the same color, I'm wondering how to create a legend representing each line once.

Sign in to comment.


Sahin Ahmed
Sahin Ahmed on 14 Apr 2020
How can I insert legends for every curves for any variable and how can I insert greekk letters in legends..... The MATLAb code is
fid1=fopen('Zeta.txt','w');
fprintf(fid1, ' Gr \t M \t Q \r\n');
fprintf(fid1,'---------------------------------------------------\r\n');
Gr(1)=5;
M(1)=5;
for i= 1:4
for j=1:1001
a(j)=M(j);
R = 2.500;
alpha = pi/4;
K = 0.41;
x1 = M(j)*M(j);
x2 = sin(alpha);
x3 = Gr(i)*x2;
x4 = x1*R;
x5 = x1*x4;
x6 = x3/x4;
x7 = x3/x5;
x8 = 2/x1;
x9 = 1 + x8;
x10 = x9*x1;
x11 = exp(M(j));
x12 = exp(-M(j));
x13 = x11 + x12;
x14 = x11 - x12;
x15 = x13/2;
x16 = x14/2;
x17 = x13 - 2;
x18 = M(j)*x14;
x19 = x17/x18;
x20 = x16/M(j);
x21 = x10*x15;
x22 = x21 - 2;
x23 = x22*x19;
x24 = x7*x23;
x25 = x9*x20;
x26 = x6*x25;
x27 = R*K;
x28 = 2*x1;
x29 = x27/x28;
x30 = x6/3;
x31 = 2*x7;
x32 = x24 - x26 + x29 + x30 + x31;
Q(j) = x32;
fprintf(fid1, '%f\t %f \t %f \r\n', Gr(i), M(j), Q(j));
Gr(i+1)=Gr(i)+10;
M(j+1)=M(j)+0.01;
end
fprintf(fid1,'-----------------------------------------------\r\n');
plot(a,Q,'r--');
%plot(x,z3,'r--')
%%plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
xlabel('M');
ylabel('Q');
title('Rate of Mass Flow');
%legend ('M=20','M=15','M=10','M=5');
hold on
end
fclose(fid1);
hold on

Community Treasure Hunt

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

Start Hunting!