How to set position of the legend outside the axes?
Mostra commenti meno recenti
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
Risposte (1)
Jonas
il 16 Set 2022
add a Name-Value pair to your legend command, e.g.
legend('Location','eastoutside')
6 Commenti
galaxy
il 16 Set 2022
Jonas
il 16 Set 2022
use a tiledlayout instead of subplot, then the two axes are aligned and have same width
tiledlayout(2,1); nexttile; plot(rand(3)); legend('Location','eastoutside'); nexttile; plot(rand(5))
galaxy
il 20 Set 2022
Jonas
il 21 Set 2022
look again at my previois comment, there you can see that the kegend is not inside the axis area. what did you expect?
galaxy
il 21 Set 2022
Categorie
Scopri di più su Legend in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


