Save plot with minimal white space without removing part of the figure

129 views (last 30 days)
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
Vivek Gupta
Vivek Gupta on 17 Jun 2019
Okay I realized the order matters. Doing axis equal and removing tick marks AFTER removing the white space doesn't result in anything being cut off (exactly as the image you posted above). However, there's still the white/gray space to the left and right of the image, and a little below the image. Do you know if it's possible in matlab to manually alter a parameter to remove this space? Of cousre I can do I can manually crop it in another software but since many plots need to be created and then saved, it would save a lot of time to do it by code in matlab. Thanks for your response.
Adam Danz
Adam Danz on 17 Jun 2019
Edited: Adam Danz on 17 Jun 2019
I think I understand your problem now. Please let me know if the solution below doesnt address the problem.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 17 Jun 2019
Edited: Adam Danz on 17 Jun 2019
Follow this demo to maximize an axis with equal aspect ratio and a colorbar within a figure.
% Set up figure size before moving forward.
% Figure units must be pixels (default).
fh = figure('Units','Pixels');
% Set axes is fill figure
ax = axes('position',[0 0 1 1]);
% create sample data
x = -10:10;
y = -10:10;
[X,Y] = meshgrid(x,y);
Z = X.^2 + Y.^2;
% plot
contourf(ax,X,Y,Z); % Use axis handle!
cbh = colorbar(ax); % Use axis handle!
cbh.Position([2,4]) = [.015,.97]; % decrease vertical size of colorbar
set(ax,'xtick',[])
set(ax,'ytick',[])
axis(ax,'equal')
% Set colorbar units and axis units to match figure units
cbh.Units = fh.Units;
ax.Units = fh.Units;
% Move axis all the way to the left
ax.Position(1) = -(ax.Position(3)-ax.Position(4))/2 + 1;
% Move colorbar to the left (the +5 at the end is pixel space between plot and colorbar)
cbh.Position(1) = sum(ax.Position([1,3])) + ax.Position(1) + 5;
% Now shrink the width of the figure; the *1.5 at the end is to factor in the y tick marks on the colorbar.
fh.Position(3) = sum(cbh.Position([1,3]))+cbh.Position(3)*1.5;
% Turn off figure border (if that's what you want to do)
fh.MenuBar = 'none';
fh.ToolBar = 'none';
190617 130015-Figure 1.jpg
  2 Comments
Vivek Gupta
Vivek Gupta on 17 Jun 2019
Yes that works great, thanks!
Any chance you can point me to a resource or explain what the position values mean for the axis, figure, and color handles? Just so I can edit this later if need be. I assume figure handle is the actual outer figure window, axis is specifically the plot, and of course the colorbar handle is for the colorbar? Is the Position vector organized as [xmin ymin xmax ymax]?
Adam Danz
Adam Danz on 17 Jun 2019
Edited: Adam Danz on 17 Jun 2019
Sure! Check out the properties listed in the "position" group for axes:
The tricky part that isn't so clear in the documentation is that once you make the axes equal, the visible border of the axes doesn't really indicate its true position. For example, in the figure I posted, the bottom, left corner is not at position (0,0) in the figure. It's lateral position is actually negative. Even though the axis appears square it's actual invisible width is wider than its height. To compensate for that, I moved the axes leftward by 1/2 of the difference between the width and height; hence,
ax.Position(1) = -(ax.Position(3)-ax.Position(4))/2 + 1;
Also recall that we're workng in pixel units rather than normalized units.

Sign in to comment.

More Answers (1)

Brad Porter
Brad Porter on 12 Jun 2020
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.

Tags

Community Treasure Hunt

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

Start Hunting!