Remove legend entries from Figure window
161 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(5 个)
Daniel Choukroun
2020-8-13
Hi,
Try property inspector > parent/child > handle visibility 'off'.
0 个评论
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';
0 个评论
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 个评论
Rosamund Herapath
2018-2-15
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
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
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!