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:
figureline1 = plot(1:10,rand(1,10),'b', 'DisplayName', 'Data 1');title('Axes 1');lg = legendtitle(lg, "my title, and it can be long")
What am I doing wrong?
Best Answer