MATLAB Answers

0

Remove data elements of the legend from figure

Marisabel Gonzalez さんによって質問されました 2019 年 4 月 9 日
最新アクティビティ Marisabel Gonzalez さんによって コメントされました 2019 年 4 月 9 日
Hi,
I have several figures in a loop-rich script in which the information of the legend would vary for most of the figures. I was wondering if there is a way of removing data elements from the legend using figure setting options. I know I can insert/remove the legent, rename the data points, change the legend appearance, etc, but I am not sure if I can remove elements from it.
For example, in the figure below I would've like to remove the last column but not by using code, instead doing it from the figure itself.
I am aware my changes won't be saved.

  0 件のコメント

サインイン to comment.

製品


リリース

R2018b

2 件の回答

Kelly Kearney
Answer by Kelly Kearney
on 9 Apr 2019
 Accepted Answer

Technically, you can delete parts of a legend, but I only know how to do it via code, not via to UI.
I should mention that using the multiple-output syntax for legend is "not recommended" by the official documentation, meaning this code could easily break in future versions of Matlab... but they've been saying that since the HG2 release in 2014b and so far I've only encountered problems in a few edge cases.
Anyway, if you really want to delete parts of a legend, here's how you can:
% An example plot, 10 to-be-labeled lines and 3 extras
h1 = plot(rand(10,9), 'ro');
hold on
h2 = plot(rand(10,3), '-k');
lbl = strtrim(cellstr(num2str((1:12)', 'data%d')));
% Label everything
[hleg, hico] = legend(lbl);
% Delete objects associated with last 3 black lines
istxt = strcmp(get(hico, 'type'), 'text');
hicot = hico(istxt);
hicol = hico(~istxt);
delete(hicot(ismember(get(hicot, 'String'), {'data10','data11','data12'})));
delete(hicol(ismember(get(hicol, 'Tag'), {'data10','data11','data12'})));
The better solution would be to just not label those lines at all from the start:
% An example plot, 10 to-be-labeled lines and 3 extras
h1 = plot(rand(10,9), 'ro');
hold on
h2 = plot(rand(10,3), '-k');
lbl = strtrim(cellstr(num2str((1:12)', 'data%d')));
% Label only the desired lines
legend(h1, lbl(1:9))

  1 件のコメント

サインイン to comment.


Adam Danz
Answer by Adam Danz
on 9 Apr 2019
Edited by Adam Danz
on 9 Apr 2019

" I was wondering if there is a way of removing data elements from the figure itself"
If you want to remove objects from a figure that has already been produced, just click on the object and press delete. That will also remove its representation in the legend.
Another option is to selection objects by using the display name in the legend. For example, these lines below will find the handle(s) to the object(s) associated with the legend name "data1" and will delete all of those objects.
allChildren = get(gca, 'Children'); % list of all objects on axes
displayNames = get(allChildren, 'DisplayName'); % list of all legend display names
% Remove object associated with "data1" in legend
delete(allChildren(strcmp(displayNames, 'data1')))
If you want to keep the object on the figure but want to remove its representation from the legend, the only way I know of doing that is to recreate the legend using the object handles to specify legend components. We can get the text in the legend and delete or edit that but we do not have access to the handles of the legend components. Most of the time, the figure can be recreated and that's the much easier approach. But if you've got a figure and no code to reporduce it, you'll have to obtain the handles of all graphic objects and then recreate the legend.

  4 件のコメント

" I was wondering if there is a way of removing data elements from the figure itself"
The legend is different from the figure.
I'll edit my answer to address that question.
What about removing it from the legend now that I edit the question?
There is no simple way of removing components of a legend from the figure without the code that produced the figure. You can rename them, but removing them isn't possible (without the changing the code and reproducing the figure).
If you have the code that generated the figure, you can edit it so that the legend appears the way you want it to -- that is much simpler.
There's probably a very complicated and error-prone way of getting all axis object handles and associating then trying to associate them with the legend text in order to re-create the legend but if the legend strings were added with calling legend instead of using 'DisplayNames', then this probably wouldn't work either.

サインイン to comment.



Translated by