Legend position changed after saving a picture

Dear All,
I have generated picture like this with legend nicely placed in the 'best' location.
screen_1.png
However, when I save it using :
saveas(gcf, savePath);
the legend automagically changed its location.
screen_2.png
Why is that and how to overcome that behavior?

Answers (2)

Hi Micholeodon,
I understand that after saving picture, legend is shifting from its position that is shown on display.
set(h,Name,Value) sets properties for the specified graphics object h using one or more name-value arguments. where Name is the property name and Value is the corresponding value.Each type of object supports a different set of properties. For example, Figure Properties, Axes Properties, Line Properties and Text Properties.
PaperPositionMode is one of the figure properties, using which one can handle displayed figure size when printing or saving. It has two modes ‘auto’ and ‘manual’.
'auto' - Printed or saved figure size matches the displayed figure size. The width and height values of the PaperPosition property equal the figure size on the display.
'manual' - Printed or saved figure size might not match the displayed figure size. Use this option if you want to print or save the figure using a size that differs from the display, or if you do not want the figure centered on the printed or saved page.
One of the possible workarounds could be by using this set method for setting PaperPositionMode.
For more understanding kindly go through the following links –

3 Comments

Thank you for your detailed answer.
Setting PaperPositionMode to 'manual' or 'auto' did not solved the problem.
Here is the code I have used to reproduce the bug.
clear; close all; clc;
rng(1) % for reproducibililty
SOLUTION_ON = 0;
% generate data
x = 1:81;
y = randn(2, numel(x));
% plot data
h = figure;
plot(x,y)
set(gca, 'XScale', 'log')
set(h, 'Position', [989 549 256 413])
legend({'data1','data2'}, 'Location', 'best') % !
%%
if SOLUTION_ON
set(h, 'PaperPositionMode', 'manual')
saveas(gcf, 'test.png');
else
saveas(gcf, 'test.png');
% save and see that in the saved image legend is located in the lower
% right corner, whereas in 'Figure 1' window legend is located in the
% upper left corner.
end % if
Your solution could be applied by setting SOLUTION_ON = 1.
Anyway thank you kindly for your contribution.
Hopefully the answer with solution will appear shortly :)
Hi,
I have reproduced your code at my end, after setting PaperPositionMode as 'auto', my display figure and saved picture are:
Display picture:
Saved picture:
As you can see legend is positioned same in both. And to use manual PaperPositionMode, you have to set PaperPosition property manually.
Thank you.
Did you change the size of the displayed figure on your end? It looks wider.

Sign in to comment.

Hello,
If you are working in linux try this
print(gcf, savePath, '-dpng','-r96','-opengl');

Asked:

on 15 May 2019

Answered:

on 8 Oct 2025

Community Treasure Hunt

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

Start Hunting!