'How can I resize the figure automatically to be tight around the plot?

I am using the axis tight command, and then copying the figure to word. The problem is that the plot is tight but the figure itself is a rectangle, and there is just a lot of space wasted for just the framework around.

So, I am copying the full figure to Paint, cutting it there for the plot only and then pasting it in Word, not fun as I have a lot of those. Any ideas?

Illustration of copied figure:

https://docs.google.com/file/d/0BwE-Ba3ePCwOb0VrSnVZcG5YMlE/edit?usp=sharing



Solution 1:[1]

Try using the Position properties of the axis directly to fit it to the figure:

>> set( gca, 'Units', 'normalized', 'Position', [0 0 1 1] );

Solution 2:[2]

I'm guessing here. But I believe you are taking the figure from some gui.

I would use copyobj to transfer the axis to another empty figure. For this you can set all the parameters as you see fit.

Solution 3:[3]

I've had the same problem, and changing Position or axis tight hasn't worked for me either, so I crop my images using ImageJ. (free)

Typically I don't have many images to crop at once, so I do them individually, but I think you could batch crop to a set size using an ImageJ macro. It'd be Process > Batch > Macro… and then in "Add Macro Code" choose Crop and specify the position and dimensions of the rectangle. (At least in ImageJ v1.46r)

I know it's not quite what you asked for, but it is a work-around. If someone else has an all-Matlab solution, I'm all ears.

Solution 4:[4]

Instead of copying the figure, have you tried saving it directly in MATLAB? In the figure window, if you go to File/Save As.., you have the option of saving as either a MATLAB .fig or in a variety of standard image formats.

Alternately, I also highly recommend Yair Altman's export_fig, which is available at https://www.mathworks.com/matlabcentral/fileexchange/23629-export_fig, which is great for creating publication quality figures.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shai
Solution 2 bdecaf
Solution 3 BLHaas
Solution 4 Jim Quirk