Remove legend entries from Figure window

조회 수: 694(최근 30일)
Milly
Milly 2011년 9월 15일
댓글: James Ratti 2021년 3월 25일
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?

답변(5개)

Daniel Choukroun
Daniel Choukroun 2020년 8월 13일
Hi,
Try property inspector > parent/child > handle visibility 'off'.

Meg Noah
Meg Noah 2020년 1월 7일
편집: Meg Noah 2020년 1월 7일
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 2016년 6월 30일
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
James Ratti
James Ratti 2021년 3월 25일
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.

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


Nando Trindade
Nando Trindade 2011년 9월 15일
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
Milly
Milly 2011년 9월 15일
Thing is I'm generating the legend by going Insert->Legend...

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


Essam Sourour
Essam Sourour 2018년 4월 22일
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.

Community Treasure Hunt

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

Start Hunting!

Translated by