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

조회 수: 3 (최근 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