Legend title won't display correctly.
Show older comments
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:

I tried using the plot editor, same behaviour.
I was expecting someting more like this:

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 Comments
dpb
on 2 Jun 2019
From the doc (emphasis added)...
[lgd,icons,plots,txt] = legend(___) additionally returns the objects used to create the legend icons, the objects plotted in the graph, and an array of the label text. This syntax is not recommended. It creates a legend that does not support some functionality, such as adding a legend title. ...
Following that advice would be my first recommendation to see if joy ensues...
Benoit Espinola
on 2 Jun 2019
Edited: Benoit Espinola
on 2 Jun 2019
Benoit Espinola
on 2 Jun 2019
Edited: dpb
on 2 Jun 2019
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...
Accepted Answer
More Answers (0)
Categories
Find more on Legend 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!