How to keep the aspect ratio fixed when putting legend outside of plot

84 次查看(过去 30 天)
As the title suggests, the ratio of the plot changes when putting a legend outside. For instance, placing the legend inside:
clear all, close all
x = 1:10;
y1 = 4*x;
y2 = 3*x + 5;
figure(1), plot(x,y1,'b',x,y2,'r')
legend('Line 1','Line 2')
Compare that with placing the legend outside:
legend('Line 1','Line 2','Location','NorthEastOutside')
Evidently, the ratio changes. Please provide suggestions if you know how to solve this problem. Your help is kindly appreciated.

回答(3 个)

Daniele la Cecilia
Daniele la Cecilia 2017-9-30
编辑:Voss 2024-11-8,17:34
x = 1:10; y1 = 4*x; y2 = 3*x + 5; figure(1), plot(x,y1,'b',x,y2,'r'), axP = get(gca,'Position'); legend('Line 1','Line 2','Location','NorthEastOutside'), set(gca, 'Position', axP)

gaurav srivastava
% %The problem can be solved by following below steps. This keeps plot area size constant:
% set unit for figure size to inches
set(gcf, 'unit', 'inches');
% get the original size of figure before the legends are added
figure_size = get(gcf, 'position')
% add legends and get its handle
h_legend = legend('legend1', 'legend)
set(h_legend, 'location', 'northeastoutside')
% set unit for legend size to inches
set(h_legend, 'unit', 'inches')
% get legend size
legend_size = get(h_legend, 'position')
% new figure width
figure_size(3) = figure_size(3) + legend_size(3)
% set new figure size
set(gcf, 'position', figure_size)
  4 个评论
Gareth Davies
Gareth Davies 2020-4-5
Why do you set the unit size to inches (or indeed to anything)? Does that do something important that not obvious?
FWDekker
FWDekker 2024-11-8,17:26
@Gareth Davies tl;dr: You want to make sure you're not mixing units.
Long version: (1) You want to make sure the figure and the legend use the same units, or else you'll be adding inches to centimetres without appropriate conversion. (2) By default the unit for the legend is 'normalized', which is its size relative to the entire figure expressed as a fraction. So if the legend h takes up 30% of the width of the figure, then h.Position(3) will equal 0.3. Meanwhile, for the figure, setting the units to 'normalized' will mean that its size is relative to your desktop resolution. In other words, even if the figure and legend both use 'normalized' as units, the units are incomparable.

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2014-8-7
legend('Line 1','Line 2','Location','best')
  4 个评论
Gunnar
Gunnar 2014-8-7
The example code in my initial post was only meant to serve as a simple example. In reality, I have a plot that is full of data, and by putting the legend inside, parts of the data will be made "invisible".
On the other hand, putting it outside will change the ratio of the true plot and skew its appearance.
Azzi Abdelmalek
Azzi Abdelmalek 2014-8-7
That's why I suggested the 'location','best'. The legend will be placed with least conflict with data in plot.
If you want to place it outside the plot, you can change the size of your plot:
plot(x,y1,'b',x,y2,'r')
h=legend('Line 1','Line 2','Location','NorthEastOutside')
set(gca,'position',[0.1 0.1 0.8 0.8])

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by