How can I remove certain points of the legend?

10 ビュー (過去 30 日間)
Max Behr
Max Behr 2020 年 6 月 20 日
コメント済み: Max Behr 2020 年 6 月 21 日
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 Center および File ExchangeLegend についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by