Add legend for a specific plot

12 views (last 30 days)
I trying to show the legend just for l1, l2 and l3, but when i write legend it show all other legends
how can I show just what i want
here my code
clear;clc
x05 = [0 0.1, 0.2, 0.3, 0.4];
y05 = [0 0.0879, 0.1404, 0.1718, 0.2031];
sdt05 = [0 0.0039, 0.0041, 0.0034, 0.0043];
errorbar(x05,y05,sdt05,'o','MarkerSize',5,...
'MarkerEdgeColor','red','MarkerFaceColor','red', 'Color', 'red')
hold on
f= polyfit(x05,y05,2);
xx = linspace(min(x05), max(x05));
v = polyval(f, xx);
l1= plot(xx,v,'red', ...
'LineWidth',2,...
'MarkerSize',10,...
'MarkerEdgeColor','red',...
'MarkerFaceColor','red', 'DisplayName', 'Rd = 0.5');
hold on
x06= [0 0.1 0.2 0.3 0.4 0.46 0.55];
y06 = [0 0.0849 0.1455 0.1829 0.2202 0.2132 0.1485];
sdt06 = [0 0.0023 0.0035 0.0044 0.0026 0.0123 0.0075];
errorbar(x06,y06,sdt06,'o','MarkerSize',5,...
'MarkerEdgeColor','green','MarkerFaceColor','green', 'Color', 'green')
hold on
f1= polyfit(x06,y06,4);
xx1 = linspace(min(x06), max(x06));
v1 = polyval(f1, xx1);
l2 = plot(xx1,v1,'green', ...
'LineWidth',2,...
'MarkerSize',10,...
'MarkerEdgeColor','green',...
'MarkerFaceColor','green','DisplayName', 'Rd = 0.6');
x064= [0 0.1 0.2 0.3 0.4 0.46 0.55];
y064 = [0 0.0849 0.1465 0.192 0.2101 0.2243 0.1728];
sdt064 = [0 0.0023 0.0019 0.0048 0.0028 0.0037 0.0051];
errorbar(x064,y064,sdt064,'o','MarkerSize',5,...
'MarkerEdgeColor','blue','MarkerFaceColor','blue', 'Color', 'blue')
hold on
f2= polyfit(x064,y064,4);
xx2 = linspace(min(x064), max(x064));
v2 = polyval(f2, xx2);
l3= plot(xx2,v2,'blue', ...
'LineWidth',2,...
'MarkerSize',10,...
'MarkerEdgeColor','blue',...
'MarkerFaceColor','blue','DisplayName', 'Rd = 0.64');
legend()

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 27 May 2022
Explicitly use the handles returned from the plotting-functions:
legend([l1(:);l2(:);l3(:)],'text1_1','text1_2','and','so','on','...')
That way you decide what plotted lines et al should be included in the legend.
(Sometime one can plot stuff outside the visible axis and use those handles to include in the legend for even further manual control)
(Once you get to this stage, it might be worthwhile to save away the handles from all plotting-functions, even those you don't use "just yet")
HTH
  2 Comments
Tesla
Tesla on 27 May 2022
Thank you very much it works
Bjorn Gustavsson
Bjorn Gustavsson on 27 May 2022
You're welcome, happy that it helped.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!