How can I remove certain points of the legend?

10 次查看(过去 30 天)
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.

回答(1 个)

Nicole Peltier
Nicole Peltier 2020-6-20
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 个评论
Max Behr
Max Behr 2020-6-21
Thanks, this workaround I tried and it works. I thought that there is a code for removing parts of the legend.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by