Showing a legend for a "group" of plots

102 views (last 30 days)
Hello, I am plotting 3 vectors (x1,x2,x3 - each has about 5 values) as lines
plot([x1 x1],[0 1],'r.-');hold on
plot([x2 x2],[0 2],'Color','blue','LineStyle','--','LineWidth',2)
plot([x3 x3],[0 2],'Color','green','LineStyle','--','LineWidth',2)
grid on; xlabel('Time(ms)');
I want to add just 3 legends for each colour. However, when I do this, it adds the 3 legends for the first 3 plots (i.e. red lines).
legend('Camera','Extreme1','Extreme2',"Location",'best','FontSize',12);
How do I show the legend just once for each colour?
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 26 Apr 2021
h1 = plot([x1 x1],[0 1],'r.-');hold on
h2 = plot([x2 x2],[0 2],'Color','blue','LineStyle','--','LineWidth',2)
h3 = plot([x3 x3],[0 2],'Color','green','LineStyle','--','LineWidth',2)
grid on; xlabel('Time(ms)');
legend( [h1(1), h2(1), h3(1)], {'Camera','Extreme1','Extreme2'},"Location",'best','FontSize',12);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!