Legend title won't display correctly.

17 次查看(过去 30 天)
Dear all,
I have been trying to make a figure with an odd number of subplots and the last subplot space would be dedicated to the legend.
This is what I came up with using Matlab 2019a:
% Construct a figure with subplots and data
figure('WindowState', 'maximized');
subplot(2,2,1);
line1 = plot(1:10,rand(1,10),'b', 'DisplayName', 'Data 1');
title('Axes 1');
subplot(2,2,2);
line2 = plot(1:10,rand(1,10),'g', 'DisplayName', 'Data 2');
title('Axes 2');
subplot(2,2,3);
line3 = plot(1:10,rand(1,10),'r', 'DisplayName', 'Data 3');
title('Axes 3');
sp = subplot(2,2,4);
% line4 = plot(1:10,rand(1,10),'y');
% title('Axes 4');
% Construct a Legend with the data from the sub-plots
plot(0,0, 0,0, 0,0, 0,0)
axis off
[lg, icons] = legend({'Data Axes 1','Data Axes 2','Data Axes 3'},'FontSize',24);
% Find the 'line' objects
icons = findobj(icons,'Type','line');
set(icons,'LineWidth', 2);
% Find lines that use a marker
icons = findobj(icons,'Marker','none','-xor');
% Resize the marker in the legend
set(icons,'MarkerSize',20);
a = get(sp,'position');
c = get(lg, 'position');
% Programatically move the Legend
newPosition = [a(1)-c(3)/2+a(3)/2 a(2)-c(4)/2+a(3)/2 c(3) c(4)];
newUnits = 'normalized';
lg.Title.Visible = 'on';
title(lg,'Particle sinking speed');
set(lg,'Position', newPosition,'Units', newUnits);
I get a string of text on the figure but it is displaying in a rather unespected way:
FB example.png
I tried using the plot editor, same behaviour.
I was expecting someting more like this:
FB example.png
Code for the second figure:
figure
line1 = plot(1:10,rand(1,10),'b', 'DisplayName', 'Data 1');
title('Axes 1');
lg = legend
title(lg, "my title, and it can be long")
What am I doing wrong?
  4 个评论
Benoit Espinola
Benoit Espinola 2019-6-2
编辑:dpb 2019-6-2
That is the issue indeed, not using the [lgd,icons,plots,txt] = legend(___) makes it possible to add the legend.
Now it is impossible to control the icons within the legend (or am I wrong?)
dpb
dpb 2019-6-2
编辑:dpb 2019-6-2
Pretty much, yes, TMW has now made the legend object almost totally opaque. :(
I've not messed too much with the internals; there are a zillion hidden properties that show up with Yair Altman's tool but there's no simple correlation with the patch and line objects; they're all buried into other structures/objects internally.
It is, indeed, pretty much a black box unfortunately.
I've complained; I'd suggest also filing bug/enhancement requests that they not be so rude to their customer base and presume they know everything...

请先登录,再进行评论。

采纳的回答

dpb
dpb 2019-6-2
编辑:dpb 2019-6-2
OK, I see I overlooked the needed (albeit hidden) property that needed most to get where you want to go...
Try exploring from this point:
figure
hL=plot(0,0, 0,0, 0,0); % three dummy lines to legend something
axis off % don't show the axis
hLg=legend({'Data Axes 1','Data Axes 2','Data Axes 3'},'FontSize',24); % so create the base legend object
hLgT=title(hLg, "My title, and it can be long"); % title, save handle to mung o
hLgAx=hLg.Axes; % retrieve the axes underlying the legend (there is still one there)
hLgLn=findobj(hLgAx,'Type','Line'); % and the handles for the lines on that axes
set(hLgLn,'LineWidth',2) % set their width property
Thanks to Yair Altman's tool for finding the hidden properties underneath the legend...and, of course, since is all undocumented, who know how long will be before TMW changes something else altho wouldn't expect the axes to go away.
This produces a full-size figure, of course, I didn't mess with the position/size, etc., but everything should work on that axes as expected; just more complicated to get access than needs must be. Why TMW is so bent on making things more rather than less difficult is hard to comprehend.
ADDENDUM
ADDENDUM 2:
There's another hidden property PlotChildren that returns the line handles directly without having to go "handle diving" under the axis altho they're in the opposite order in the two arrays for some reason.
> hLgLn=findobj(hLgAx,'Type','Line')
hLgLn =
3×1 Line array:
Line (Data Axes 3)
Line (Data Axes 2)
Line (Data Axes 1)
>> hLg.PlotChildren
ans =
3×1 Line array:
Line (Data Axes 1)
Line (Data Axes 2)
Line (Data Axes 3)
>> all(flip(hLgLn)==hLg.PlotChildren)
ans =
logical
1
>>

更多回答(0 个)

类别

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

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by