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.
Saving or Copying a Single Plot
Create a contour plot of the peaks
function with a title and
a colorbar.
contour(peaks)
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.
Alternatively, you can save the content using the exportgraphics
function. 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; exportgraphics(ax,'myplot.png','Resolution',300)
The copygraphics
function provides similar functionality for copying
content to the clipboard.
ax = gca;
copygraphics(ax,'Resolution',300)
Saving or Copying Multiple Plots in a Figure
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.
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.
t = tiledlayout(2,2,'TileSpacing','Compact','Padding','Compact'); nexttile plot([0 1]) nexttile plot([1 0]) nexttile plot([0 1 0 1]) nexttile plot([1 0 1 0])
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.
exportgraphics(t,'fourplots.pdf','BackgroundColor','none')
Alternatively, you can copy the layout to the clipboard using the
copygraphics
function.
copygraphics(t,'BackgroundColor','none')