Remove legend entries from Figure window
163 views (last 30 days)
Show older comments
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?
0 Comments
Answers (5)
Daniel Choukroun
on 13 Aug 2020
Hi,
Try property inspector > parent/child > handle visibility 'off'.
0 Comments
Meg Noah
on 7 Jan 2020
Edited: Meg Noah
on 7 Jan 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';
0 Comments
Muhammed Fasil
on 30 Jun 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 Comments
Rosamund Herapath
on 15 Feb 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
on 25 Mar 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.
Nando Trindade
on 15 Sep 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
Essam Sourour
on 22 Apr 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.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!