Save plot with minimal white space without removing part of the figure
Show older comments
In order to save MATLAB plots with minimal white space I have been using the code from this resource,with the code copied below.
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
However, part of the figure is now cut off (the 0 on the axis bar). Is there a simple solution to this problem?

Edit: also there is still white space at the left and right edges. It is hard to see extra white space so I also attached the image. Of course I can remove this manually, but since I am processing many images it would be much faster to do it automatically.
Thanks!
5 Comments
Adam Danz
on 11 Jun 2019
I could not recreate the problem. When I add a colorbar either before or after the resize, the upper and lower ends are not cut off.
Is there another section of your code that sets axis to equal or square?
Vivek Gupta
on 17 Jun 2019
Adam Danz
on 17 Jun 2019
I ran all 3 sections of your code from the comment above and still cannot recreate the problem. I'm using r2019a.

Vivek Gupta
on 17 Jun 2019
Accepted Answer
More Answers (1)
Brad Porter
on 12 Jun 2020
1 vote
If you are not trying to develop a flexible application and just need a quick solution, just adjust the numbers in the following:
figure()
set(gca,'position',[0.07 0.07 0.92 0.88]);
done.
1 Comment
Hai-Po Chan
on 29 Sep 2022
This works great for me. Thank you!
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!