Main Content

Saving and Copying Plots with Minimal White Space

One way to minimize the white space when saving or copying the contents of a plot is to use the axes toolbar, which appears when you hover over the upper right corner of the axes. An alternative method is to use the exportgraphics and copygraphics functions, which provide more flexibility.


The following examples use the exportgraphics and copygraphics functions, which are new in R2020a. If you are using an earlier release, see Save Plots with Minimal White Space (19b).

Saving or Copying a Single Plot

Create a contour plot of the peaks function with a title and a colorbar.

title('Peaks Function')

Save the plot to a file by hovering over the export button in the axes toolbar and selecting the first item in the drop-down list. If you want to copy the contents of the plot to the clipboard, select either the second or the third item in the drop-down list. The second item copies the content as an image, and the third items copies the content as a vector graphic. The content you save or copy is tightly cropped around the title, the axes, and the colorbar.

Contour plot with the export drop-down list expanded in the axes toolbar

Alternatively, you can save the content using the exportgraphics function, which is available starting in R2020a. This function provides the same tight cropping around your content, and it also provides additional options. For example, you can save an image file and specify the resolution.

ax = gca;
% Requires R2020a or later

The copygraphics function provides similar functionality for copying content to the clipboard.

ax = gca;
% Requires R2020a or later

Saving or Copying Multiple Plots in a Figure

Starting in R2019b, you can create a tiling of plots in a figure using the tiledlayout function. This function has options for minimizing the space around the plots. (If you are using an earlier release, you can use the subplot function to create a tiling of plots. However, the subplot function does not have options for controlling the space around the plots.)

Create a 2-by-2 tiled chart layout by calling the tiledlayout function. To minimize the space between the plots, set the 'TileSpacing' name-value pair argument to 'compact'. To minimize the space around the perimeter of the layout, set the 'Padding' name-value pair argument to 'compact'. Next, call the nexttile function to create the first axes, and call the plot function to plot into the axes. Then, create three more axes and plots.

% Requires R2019b or later
t = tiledlayout(2,2,'TileSpacing','Compact','Padding','Compact');
plot([0 1])
plot([1 0])
plot([0 1 0 1])
plot([1 0 1 0])

2-by-2 tiled chart layout with compact tile spacing and padding

Save the layout as a PDF file by passing the tiled chart layout (t) to the exportgraphics function. In this case, save the PDF with a transparent background.

% Requires R2020a or later

Alternatively, you can copy the layout to the clipboard using the copygraphics function.

% Requires R2020a or later

See Also



Related Topics