How to add legend for graph with multiple lines on it

조회 수: 5 (최근 30일)
Benjamin Nienhouse
Benjamin Nienhouse 2020년 11월 19일
답변: Srivardhan Gadila 2020년 11월 24일
Hey, so I'm wondering you I would add a legend to all the graphs, I would like to label each line on each graph and I'm not sure how to do that.
Thanks,
Ben.
h = input("Initial height (m): "); %Initial height
angle = input("Angle of elavation: "); %Degrees
v = input("Initial velocity (m/s): "); %M/S
g=-9.81; %M/S^2
angles = 0:10:180;
heights = 0:10:100;
velocity = 10:10:100;
figure
for a = angles
[x_val,y_val] = Arc(g,h,a,v);
plot(x_val,y_val);
hold on;
end
xlabel 'x (m)'
ylabel 'y (m)'
title 'Angles 0-180'
figure
for hi = heights
[x_val,y_val] = Arc(g,hi,angle,v);
plot(x_val,y_val);
hold on;
end
xlabel 'x (m)'
ylabel 'y (m)'
title 'Height 0-100 (m)'
figure
for vel = velocity
[x_val,y_val] = Arc(g,h,angle,vel);
plot(x_val,y_val);
hold on;
end
xlabel 'x (m)'
ylabel 'y (m)'
title 'Velocity 10-100 (m/s)'
m = [x_val',y_val'];
max_val = max(max(m));
figure
plot(x_val,y_val);
hold on;
[~,idx] = max(y_val);
plot(x_val(idx),y_val(idx),'r*')
text(x_val(idx),y_val(idx),'max','VerticalAlignment','Bottom','HorizontalAlignment','Left','FontSize',8)
axis([0 max_val*1.05 0 max_val*1.05]);
xlabel 'x (m)'
ylabel 'y (m)'
title 'Original Data'
function [x_val,y_val] = Arc(g,h,angle,v)
y = h;
t = 0;
x_val = [];
y_val = [];
while y >= 0
y=.5*g*t.^2+v*sind(angle)*t+h;
x=v*cosd(angle)*t;
t = t + 0.01;
x_val = [x_val,x];
y_val = [y_val,y];
end
end
  댓글 수: 1
dpb
dpb 2020년 11월 19일
Simply call legend for each plot when you create it...

댓글을 달려면 로그인하십시오.

답변 (1개)

Srivardhan Gadila
Srivardhan Gadila 2020년 11월 24일
Refer to the documentation of Add Legend to Graph & legend.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by