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

1 次查看(过去 30 天)
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)

采纳的回答

Henriette Larsen
Henriette Larsen 2021-2-14
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

更多回答(1 个)

randerss simil
randerss simil 2021-2-13
编辑:randerss simil 2021-2-13
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 个评论
Henriette Larsen
Henriette Larsen 2021-2-13
编辑:Henriette Larsen 2021-2-13
@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

请先登录,再进行评论。

类别

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

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by