MATLAB Answers

How can I remove certain points of the legend?

조회 수: 1(최근 30일)
Max Behr
Max Behr 20 Jun 2020
댓글: Max Behr 21 Jun 2020
I got a plot with two curves and I use this command:
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.

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Nicole Peltier
Nicole Peltier 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
hold on;
% 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 21 Jun 2020
Thanks, this workaround I tried and it works. I thought that there is a code for removing parts of the legend.

댓글을 달려면 로그인하십시오.




Community Treasure Hunt

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

Start Hunting!

Translated by