How do I get rid of space above figure?

조회 수: 33 (최근 30일)
Tobias Frederiksen
Tobias Frederiksen . 2022년 4월 12일
답변: Richard Quist . 2022년 5월 2일
I am trying to print and save the figure as an EPS-file. This works fine but the figure comes out with a hugh blank space above it.
Can someone help me remove this?

답변 (2개)

Riccardo Scorretti
Riccardo Scorretti 2022년 4월 12일
편집: Riccardo Scorretti 님. 2022년 4월 12일
Hi Tobias,
I had a similar problem (but I save images in .png format). I can propose the method I solved it:
  1. I save the image with a resolution of 300 dpi by using: print my_wonderful_image.png -dpng -r300
  2. I import the image by using Gimp, I crop the image and save back to the .png format.
It's not elegant, it's rather messy, but it works. I can use the images generated in this way with both LaTeX and MS Word, event to make A0 format posters, and the size of files are reasonable. In the images below: before cropping (left) and after cropping (right). The size of the two images is approximately the same, but the image on the left has some white space.
Of course this method will not work if you need to save the whole figure (= with the title, and the GUI elements). In this case, you can tweak the figure by modifying the property Position of the axes:
% Create the figure
t = linspace(0, 2*pi, 256);
plot(t, sin(t));
xlabel('t', 'FontSize', 16);
ylabel('sin(t)', 'FontSize', 16);
% Tweak the position
ax = gca ; ax.Position = [0.11 0.11 0.87 0.87];
Hereafter the two figures (= before and after tweaking the position):

Richard Quist
Richard Quist 2022년 5월 2일
MATLAB-generated EPS files are usually tightly cropped by default, which should have eliminated that extra space above your plot in the output file. Using either of the following should ensure that this occurs:
  1. use MATLAB's exportgraphics command which was introduced in R202a (first example below)
  2. Set the figure renderer to 'painters' before saving (second example below)
% This example assumes that 'fig' is the handle to your figure.
% In R2020a and later, you can use the exportgraphics command and specify
% the 'ContentType' as 'vector' (to ensure tightly cropped, scalable output):
exportgraphics(fig, 'output.eps', 'ContentType', 'vector');
% This example assumes that 'fig' is the handle to your figure.
% Change the figure renderer to 'painters' before saving the file
fig.Renderer = 'painters';
I hope that helps.




Community Treasure Hunt

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

Start Hunting!

Translated by