How to set position of the legend outside the axes?

조회 수: 1,767 (최근 30일)
galaxy
galaxy 2022년 9월 16일
편집: Adam Danz 2023년 9월 26일
Hi all,
I create sample code for set legend position.
f = figure;
p = uipanel('Title','Panel','FontSize',12,...
'BackgroundColor','white',...
'Position',[.1 .1 .67 .8]);
% Construct a figure with subplots and data
subplot(2,1,1, 'Parent', p);
line1 = histogram(rand(1,1000));
title('Axes 1');
subplot(2,1,2, 'Parent', p);
line2 = histogram(rand(1,1000));
title('Axes 2');
% Construct a Legend with the data from the sub-plots
hL = legend([line1,line2],{'Data Axes 1','Data Axes 2'});
%set(hL,'location', 'bestoutside');
Here is result:
But, I want to move legend to outside of axes such as:
Do anyone know? Please help me.
Thank you so much

답변 (1개)

Jonas
Jonas 2022년 9월 16일
add a Name-Value pair to your legend command, e.g.
legend('Location','eastoutside')
  댓글 수: 6
Jonas
Jonas 2022년 9월 21일
look again at my previois comment, there you can see that the kegend is not inside the axis area. what did you expect?
galaxy
galaxy 2022년 9월 21일
sorry, I think in your code, legend only outside of plot area.
I changed some lines. So can you move legend same following figure.
Thank you so much
f = figure;
p = uipanel(f, 'Position',[.1 .1 .8 .8], 'Title','Panel 1');
p1 = uipanel('Parent', p, 'Position',[.1 .1 .6 .6], 'Title','Panel 2');
t = tiledlayout(p1,1,1);
nexttile(t);
plot(rand(3));
h = legend('Location','northeastoutside');

댓글을 달려면 로그인하십시오.

카테고리

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