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

2 views (last 30 days)
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)');

Accepted Answer

emehmetcik
emehmetcik on 18 Feb 2015
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)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!