Reordering legend items deletes legend handle - how can I get them back?

2 次查看(过去 30 天)
For the legend of a complex plot, I wish to:
1) reorder the items so that they better match the order of data points in the plot
2) increase the legend marker sizes
2) on its own is easy enough, but I found that 1) involves a step that apparently deletes the icons part of the legends handle. Consider the following MWE:
x1 = randn(1,10);
x2 = randn(1,10);
plot(1:10, x1, 'ko', 1:10, x2, 'bo')
[lh,icons] = legend('x1', 'x2');
neworder = 2:-1:1; % reverse order of items (markers + their text) in the legend, so that this nicely matches the order (when viewing top-down) of plotted elements; the default order is reversed, for some reason
icons(4).MarkerSize = 22; % x1
icons(6).MarkerSize = 22; % x2; this is sometimes icons(3).Children.MarkerSize ?!
lh.AutoUpdate = 'off';
lh.PlotChildren = lh.PlotChildren(neworder);
icons(4).MarkerSize = 22; % attempt to re-set this
[lhNEW,iconsNEW] = findobj(gcf, 'Type', 'Legend');
iconsNEW(6).MarkerSize = 22;
The last command gives
Unrecognized property 'MarkerSize' for class 'matlab.graphics.primitive.Data'.
>> icons(4)
ans =
handle to deleted Data
Getting the handles for the created legend by using findobj doesn't work either:
[lhNEW,iconsNEW] = findobj(gcf, 'Type', 'Legend');
iconsNEW(6).MarkerSize = 22;
No method 'findobj' with matching signature found for class 'matlab.ui.Figure'.

采纳的回答

Voss
Voss 2022-5-25
Maybe you will have better luck modifying the order of the lines and their names in the call to legend, rather than reordering the objects in the legend after it is created:
x1 = randn(1,10);
x2 = randn(1,10);
h_line = plot(1:10, x1, 'ko', 1:10, x2, 'bo');
names_line = {'x1', 'x2'};
neworder = 2:-1:1; % reverse order of items (markers + their text) in the legend, so that this nicely matches the order (when viewing top-down) of plotted elements; the default order is reversed, for some reason
[lh,icons] = legend(h_line(neworder),names_line(neworder));
icons(4).MarkerSize = 22;
icons(6).MarkerSize = 22;
  2 个评论
Steven Lord
Steven Lord 2022-5-25
Rather than having the line handles and names in separate variables, I'd probably set the lines' DisplayName properties. I'd also be wary about calling legend with more than one output; as its documentation page states that has not been recommended since release R2014b.
x1 = randn(1,10);
x2 = randn(1,10);
h_line = plot(1:10, x1, 'ko', 1:10, x2, 'bo');
h_line(1).DisplayName = 'x1';
h_line(2).DisplayName = 'x2';
neworder = 2:-1:1; % reverse order of items (markers + their text) in the legend
% so that this nicely matches the order (when viewing top-down) of plotted elements;
% the default order is reversed, for some reason
[lh,icons] = legend(h_line(neworder)); % No names needed in this call
icons(4).MarkerSize = 22;
icons(6).MarkerSize = 22;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by