Global legend for all subplots in a figure placed ON the figure (not outside the figure)?

2 views (last 30 days)
Hello :)
So, I'd like to make one legend on the second subplot (the big one to the right in the figure) and it has to be one legend for all three graphs with the red one appearing first.
BUT when I try the following as shown by the code below, I can only get the legend placed above the first subplot but below the second subplot, as if the legend belongs to the first subplot, so giving the legend a new position does not solve the problem.
Any help to solve this will be appreciated, thanks :)
Best regards, Henriettte
figure
ax1 = subplot(1,2,1);
hold on
p1 = plot(time1,temp1,'.-','MarkerSize',10,'Color',Cdefault(2,:));
P1 = get(gca, 'Position');
set(gca,'Units','Normalized','Position',P1.*[1 0.7 0.45 0.7])
xlabel('Time [min]','Interpreter','latex')
ylabel('Surface temperature [$^{\circ}$C]','Interpreter','latex')
ax2 = subplot(1,2,2);
p2 = plot(time2,temp2,'.-','MarkerSize',10,'Color',[0.2 0.6 1.0]);
hold on
p3 = plot(time3,temp3,'.-','MarkerSize',10,'Color',[0.0 0.0 0.8]);
P2 = get(gca, 'Position');
set(gca,'Units','Normalized','Position',P2.*[0.55 0.7 2 0.7])
xlim([0 45])
xlabel('Time [min]','Interpreter','latex') % % %
linkaxes([ax1,ax2],'y');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.05, 0.7, 1.0])
yticklabels(ax2,{})
t.TileSpacing = 'none';
legend([p1,p2,p3],{'Brake test on: Heating',...
'Brake test off: Natural cool down',...
'Brake test off + Cooling system on: Forced cool down',...
'Location','NorthEast'})
hold off
(specifications to 'prettyfy' the figure has been left out in the code)

Accepted Answer

Henriette Larsen
Henriette Larsen on 14 Feb 2021
In the mean time I have come around the problem in another way, that is by making a horisontal legend on top instead. It´s not what I wanted but looks okay, so this is fine.
/Henriette

More Answers (1)

randerss simil
randerss simil on 13 Feb 2021
Edited: randerss simil on 13 Feb 2021
figure
ax1 = subplot(2,1,1);
...% your code
....
xlabel('Time [min]','Interpreter','latex')
ylabel('Surface temperature [$^{\circ}$C]','Interpreter','latex')
ax2 = subplot(2,1,2);
...
Try subplot with something like above,
  1 Comment
Henriette Larsen
Henriette Larsen on 13 Feb 2021
Edited: Henriette Larsen on 13 Feb 2021
@randerss simil, sry, I don't see how that solves anything... I am already using subplot. The problem is getting the legend on top of/in front of the second subplot (i.e. not hiding behind it like now as shown in my figure). Please explain your reasoning, thanks :)
/Henriette

Sign in to comment.

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!