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

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 Commenti

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?
Thanks for your response.
I realized the problem could be because I am also removing the x and y labels and tick marks. So, specifically, the following code doesn't give me an issue:
% create sample data
x = -10:10;
y = -10:10;
[X,Y] = meshgrid(x,y);
Z = X.^2 + Y.^2;
% plot
contourf(X,Y,Z);
colorbar;
% remove white space
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, removing ticke marks with
% remove tick marks and labels
set(gca,'xtick',[])
set(gca,'ytick',[])
Makes the limits of the color axis bar to be cut off. And you're definitely right that adding
axis equal
leaves white space on the left and right-hand side.
Do you know if there is any way to remove the axis labels / tick emarks, make the axis equal scaling, and remove all white space (up to the extent the 0 on the axis bar is not cut off) Thanks for your help.
I ran all 3 sections of your code from the comment above and still cannot recreate the problem. I'm using r2019a.
190617 013110-Figure 1.jpg
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.
I think I understand your problem now. Please let me know if the solution below doesnt address the problem.

Accedi per commentare.

 Risposta accettata

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 Commenti

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]?
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.

Accedi per commentare.

Più risposte (1)

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.

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by