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

72 visualizzazioni (ultimi 30 giorni)
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.

Risposte (3)

Daniele la Cecilia
Daniele la Cecilia il 30 Set 2017
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
gaurav srivastava il 4 Lug 2018
% %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)
  3 Commenti
Sim
Sim il 15 Mar 2020
Modificato: Sim il 15 Mar 2020
Hi, I got a similar problem when I had to put a legend - with variable size - outside the plot.
In my case, I had many figures with a variable legend size, for each figure.
My goal was to keep the plot size constant in each figure and just changing the legend size (in particular the width, through the number of columns).
I found that the Azzi Abdelmalek's solution worked for my case (i.e. keeping the plot axes size and position constant for all my figures), while I found that the Gaurav Srivastava's solution did not work for my case.
You can check both solution by changing the
h.NumColumns
from 1 to 4, to see the differences between the two proposed solution. Here following the code I used:
% input data
x = 1:10;
y1 = 4*x;
y2 = 3*x + 5;
y3 = 2*x - 2;
y4 = 3*x.^2;
% Gaurav Srivastava's solution
figure('position',[10 10 1300 500],'Color',[1 1 1]);
plot(x,y1,'b',x,y2,'r',x,y3,'m',x,y4,'g')
set(gcf,'unit','inches');
figure_size = get(gcf,'position');
h = legend('legendfsdjfsdofodfodfosydfdfysdfidyfdhfdfhfldfhfjfhd1',....
'fjsdjfsdfjdjfdjsfhjdfhfsdhfhsdfsdhfosfhsohfjdfhjf',...
'qqqqqqqqQQQQQQOPPPPOQPOPQOPOQPOQPOQPOQPOQPOPQOQPQOPQOQPOPQO',...
'legendfffffdfsddfdfdfsffffsdfbnsdbfnsdbfnmsbfnffdfdf');
h.NumColumns = 4;
set(h, 'location', 'northeastoutside')
set(h, 'unit', 'inches')
legend_size = get(h, 'position');
figure_size(3) = figure_size(3) + legend_size(3);
set(gcf, 'position', figure_size)
% Azzi Abdelmalek's solution
figure('position',[10 10 1300 500],'Color',[1 1 1]);
plot(x,y1,'b',x,y2,'r',x,y3,'m',x,y4,'g')
h = legend('legendfsdjfsdofodfodfosydfdfysdfidyfdhfdfhfldfhfjfhd1',....
'fjsdjfsdfjdjfdjsfhjdfhfsdhfhsdfsdhfosfhsohfjdfhjf',...
'qqqqqqqqQQQQQQOPPPPOQPOPQOPOQPOQPOQPOQPOQPOPQOQPQOPQOQPOPQO',...
'legendfffffdfsddfdfdfsffffsdfbnsdbfnsdbfnmsbfnffdfdf');
h.NumColumns = 4;
set(h, 'location', 'northeastoutside');
set(gca,'position',[0.1 0.1 0.2 0.8])
Gareth Davies
Gareth Davies il 5 Apr 2020
Why do you set the unit size to inches (or indeed to anything)? Does that do something important that not obvious?

Accedi per commentare.


Azzi Abdelmalek
Azzi Abdelmalek il 7 Ago 2014
legend('Line 1','Line 2','Location','best')
  4 Commenti
Gunnar
Gunnar il 7 Ago 2014
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 il 7 Ago 2014
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])

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by