Remove legend entries from Figure window

159 visualizaciones (últimos 30 días)
Milly
Milly el 15 de Sept. de 2011
Comentada: James Ratti el 25 de Mzo. de 2021
I am editing my figure graphically in the figure window, and I'd like to know how to remove legend entries. (I used plottools in the command line to open up all the editing windows like Plot Browser, Property Editor, Figure Pallette.), and created a legend automatically by clicking on the legend button.
I have 18 vectors plotted on the same axes, but they are divided into 3 colours, so I need the legend to show only 3 legend entries rather than all 18.
The only way I know to remove entries is very laborious: 1) select the data you wish to delete in the figure editor (can only select ONE vector at a time!) 2) type into the command line: hasbehavior(gco, 'legend', false); 3) right click on the legend-> refresh
There must be an easier way?

Respuestas (5)

Daniel Choukroun
Daniel Choukroun el 13 de Ag. de 2020
Hi,
Try property inspector > parent/child > handle visibility 'off'.

Meg Noah
Meg Noah el 7 de En. de 2020
Editada: Meg Noah el 7 de En. de 2020
Here's an example:
clc
close all
clear all
% fake data
f = -pi:0.01:pi;
y1 = sin(2.*f + pi/3) + 0.05*rand(size(f));
y2 = sin(3.*f + pi/2) + 0.05*rand(size(f));
y3 = sin(2.5.*f + 0) + 0.05*rand(size(f));
y4 = sin(0.75.*f + pi/6) + 0.05*rand(size(f));
% code
figure;
l1 = plot(f, y1, 'r','DisplayName','My Data 1');
hold on
l2 = plot(f, y2, 'color', [0.8 0.7 0],'DisplayName','My Data 2');
l3 = plot(f, y3, 'b','DisplayName','My Data 3');
l4 = plot(f, y4, 'color', [0 0.75 0],'DisplayName','My Data 4');
hleg = legend('location','best');
hold off
xlabel('xdata [radians]');
ylabel('ydata');
ylim([-1.2 1.2]);
xlim([-pi pi]);
title('just some random data');
% how to: specify just certain lines
hleg = legend([l2 l3 l4],'location','best');
% how to: rename the data, change text properties
hleg.String = {'Random 2','Random 3','Random 4'};
hleg.FontName = 'Ariel';
hleg.FontSize = 12;
hleg.FontWeight = 'bold';
Or for your application
clc
close all
clear all
% fake data
f = -pi:0.01:pi;
y1 = sin(2.*f + pi/3) + 0.05*rand(size(f));
y2 = sin(3.*f + pi/2) + 0.05*rand(size(f));
y3 = sin(2.5.*f + 0) + 0.05*rand(size(f));
y4 = sin(0.75.*f + pi/6) + 0.05*rand(size(f));
% code
figure;
l1 = plot(f, y1, 'r','DisplayName','My Red Data');
hold on
l2 = plot(f, y2, 'r','DisplayName','My Red Data');
l3 = plot(f, y3, 'b','DisplayName','My Blue Data');
l4 = plot(f, y4, 'b','DisplayName','My Blue Data');
hleg = legend('location','best');
hold off
xlabel('xdata [radians]');
ylabel('ydata');
ylim([-1.2 1.2]);
xlim([-pi pi]);
title('just some random data');
% how to: specify just certain lines
hleg = legend([l1 l3],'location','best');
% how to: rename the data, change text properties
hleg.String = {'Random Red','Random Blue'};
hleg.FontName = 'Ariel';
hleg.FontSize = 12;
hleg.FontWeight = 'bold';

Muhammed Fasil
Muhammed Fasil el 30 de Jun. de 2016
Right click on Legend>Show Property editor>More properties>string and make the modifications you wanted to make like deleting a legend or inserting a legend etc
  2 comentarios
Rosamund Herapath
Rosamund Herapath el 15 de Feb. de 2018
A good answer, but I can only delete the last items on a legend this way. Is there any way to remove the top entry?
James Ratti
James Ratti el 25 de Mzo. de 2021
When I try this, it deletes the legend name, but not the associated color/symbol key for the entries. So if I have 7 entries and want to hide the names/keys for 4 of them by modifying the strings, the other 3 just move to the top of the list and become disconnected from the color/symbol keys they're supposed to represent. I can't find a way around this without the command line.

Iniciar sesión para comentar.


Nando Trindade
Nando Trindade el 15 de Sept. de 2011
Can you order your plotting so that all 3 different colors get called first?
you can do something like?
plot(..,'r') hold on plot(..,'b') plot(..,'g') plot(rest)
legend_str= {'1', '2', '3'} legend(legend_str, 'Location', 'Best')
if not you can plot the 3 different colors at 0,0 or something so that they would be the first 3 in the legend and do what i have above for the legend_str
  1 comentario
Milly
Milly el 15 de Sept. de 2011
Thing is I'm generating the legend by going Insert->Legend...

Iniciar sesión para comentar.


Essam Sourour
Essam Sourour el 22 de Abr. de 2018
You can copy this figure to another temporary figure, delete all plots in the new figure, then start copy paste from the original figure to the empty figure in the order you like.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by