Save plot with minimal white space without removing part of the figure
71 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Adam Danz
2019-6-17
编辑:Adam Danz
2019-6-17
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';
2 个评论
Adam Danz
2019-6-17
编辑:Adam Danz
2019-6-17
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.
更多回答(1 个)
Brad Porter
2020-6-12
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!