Does anybody know how to include a legend in a figure of three functions with different colours or patterns?

1 次查看(过去 30 天)
Hi, I am plotting three functions at the same time in order to compare them and I would like to include a legend in this graphic, to represent the color and correspondet name of each function. Does anybody know how to do it?
plot(t,x1,'r'); hold on; plot(t,x2,b--o'); hold on plot(t,x3,g--');
Thank you!!!
title('Comparison'); xlabel('Time'); ylabel('f(t)');
subplot(132); plot(t1,x_rec_vel(:,2),'r'); hold on; plot(t1,Y(1:end-1,5),'b--o'); title('Velocity Mass 2'); xlabel('Time'); ylabel('v(t)');
subplot(133); plot(t1,x_rec_vel(:,3),'r'); hold on; plot(t1,Y(1:end-1,6),'b--o'); title('Velocity Mass 3'); xlabel('Time'); ylabel('v(t)');

采纳的回答

emehmetcik
emehmetcik 2015-2-18
doc legend
plot(t,x1,'r');
hold on; % One 'hold on' is enough
plot(t,x2,b--o');
plot(t,x3,g--');
legend('Function-1', 'Function-2', 'Function-3')
... same for other figures as well.
You can use this form as well:
h1(1) = plot(t,x1,'r', 'displayName', 'Function-1');
hold on; % One 'hold on' is enough
h(2) = plot(t,x2,b--o', 'displayName', 'Function-1');
h(3) = plot(t,x3,g--', 'displayName', 'Function-1');
legend(h)

更多回答(0 个)

类别

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