Legend title won't display correctly.
이전 댓글 표시
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
dpb
2019년 6월 2일
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
2019년 6월 2일
편집: Benoit Espinola
2019년 6월 2일
Benoit Espinola
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...
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!