How can I remove certain points of the legend?

10 views (last 30 days)
Max Behr
Max Behr on 20 Jun 2020
Commented: Max Behr on 21 Jun 2020
Hello,
I got a plot with two curves and I use this command:
legend('1.Try','2.Try')
If I add some significance lines+stars, matlab mentions them in legend as "data1", "data2", etc.
plot(xt([2 3]), [1 1]*max(yt)*1.05, '-k', mean(xt([2 3])), max(yt)*1.1, '*k')
How can I remove the "data1" and "data2" from the legend without removing the lines itself?
Thanks you.

Answers (1)

Nicole Peltier
Nicole Peltier on 20 Jun 2020
If the lines you want in the legend are the first things you plot (i.e., you plot all of them before you add the extra lines and annotations), you can call legend after everything is plotted with only the legend entries you want to be shown. Example below:
% Sample data to plot
x = 0:10;
y1 = x.^2;
y2 = x.^3;
% Plot data
plot(x,y1);
hold on;
plot(x,y2);
% Add other lines and annotations
line(xlim, [10 10]);
% Create legend for y1 and y2
% Since there are only two entries, nothing is shown in the legend for the reference line
legend('y1', 'y2');
If you're plotting additional annotations between lines of plotting data, you should save plots to variables, as below:
% Plot line 1
h1 = plot(x,y1);
hold on;
% Other lines and annotations
line(xlim, [10 10]);
% Plot line 2
h2 = plot(x,y2);
% Create legend
legend([h1 h2], 'y1', 'y2');
Hope this helps!
  1 Comment
Max Behr
Max Behr on 21 Jun 2020
Thanks, this workaround I tried and it works. I thought that there is a code for removing parts of the legend.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!