How can I remove certain points of the legend?

13 visualizaciones (últimos 30 días)
Max Behr
Max Behr el 20 de Jun. de 2020
Comentada: Max Behr el 21 de Jun. de 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.

Respuestas (1)

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

Iniciar sesión para comentar.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by