How to Crop a PGFPlots Figure with the Standalone Class
At a glance
- 1. Use standalone mode
Compile the TikZ picture as a standalone document so its page follows the picture’s bounding box.
- 2. Set the outer margin
Adjust the standalone border option to add or remove padding around the complete figure.
- 3. Tune the axis
Control axis dimensions, limits, and descriptions separately; they can create whitespace inside the cropped page.
- 4. Check the PDF page
Inspect the page dimensions to distinguish actual margins from whitespace shown by a PDF viewer.
A PDF made with the standalone class can be cropped to the dimensions of a PGFPlots picture, rather than having a full letter- or A4-sized page. The key distinction is that standalone crops to the TikZ picture’s bounding box, not just to the visible data. Axis labels, tick labels, legends, and other nodes are part of that box, so they can make the result larger than the plotted rectangle.
Make a cropped PGFPlots PDF
Use the tikz class option to enable standalone’s TikZ mode, load pgfplots, and put the axis inside a tikzpicture. For example:
\documentclass[tikz,border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=7cm,
height=4cm,
xlabel={$x$},
ylabel={$y$},
xmin=0, xmax=4,
ymin=0, ymax=16,
domain=0:4,
samples=50
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
Compile this as a PDF. The resulting page is sized around the picture, with the extra margin specified by border=2pt. You can change that value, for example to border=0pt for no added border or border=5pt for more padding. The border adds space around the entire TikZ bounding box; it does not change the axis’s own layout.
Understand where the whitespace comes from
A PGFPlots axis includes more than the rectangle where data are drawn. Its bounding box normally includes the tick marks and tick labels, axis labels, title, legend, and any annotations. These elements need room, so the cropped PDF may have a wider or taller page than the axis rectangle itself. This is expected: standalone is preserving the complete figure rather than cutting off its text.
If the whitespace is outside the complete picture, reduce the standalone border. If it is inside the page around the plotted data, adjust the axis. For example, explicitly set limits to avoid automatic padding around the data:
\begin{axis}[
xmin=0, xmax=4,
ymin=0, ymax=16,
enlargelimits=false
]
enlargelimits=false disables the extra expansion of axis limits. It does not remove space needed for tick labels or axis labels. If the data range is not the range you want to show, explicit xmin, xmax, ymin, and ymax values give more direct control.
Set plot dimensions predictably
By default, PGFPlots’ width and height describe the overall axis dimensions, including descriptions. If you want those dimensions to refer to the plotting rectangle, use scale only axis:
\begin{axis}[
width=7cm,
height=4cm,
scale only axis,
xlabel={$x$},
ylabel={$y$}
]
The labels and tick text are then placed outside the specified 7 cm by 4 cm rectangle, and the complete TikZ picture—and therefore the standalone page—will be larger. This option makes plot-area sizing predictable; it is not a command to crop away labels.
For a row or column of plots that must align at the plot-area edges, PGFPlots also provides trim axis left and trim axis right. These options alter the axis contribution to the bounding box by trimming at the corresponding side. Use them only when you intend to align or crop descriptions at those edges: tick labels or other content can extend beyond the trimmed boundary and may be cut off in the resulting PDF or when included elsewhere.
Common mistakes
- Setting
border=0ptto remove all internal gaps. This removes only standalone’s added margin. It does not remove space for ticks, labels, titles, or legends. - Confusing axis size with page size. With
scale only axis, the requested width and height apply to the data rectangle, not the full page. Text outside it increases the final page dimensions. - Cropping based only on visible marks. The bounding box is a layout boundary, not an ink-detection crop. It may include space reserved for labels or nodes even if they contain little visible content.
- Blaming the PDF viewer’s display. Viewers may show a neutral area around the page. Check the PDF page dimensions or include the PDF in another document before changing the plot; the viewer background is not necessarily part of the PDF.
- Trimming axis descriptions without checking them. The trim options are useful for carefully aligned multi-panel figures, but can clip tick labels or axis labels if the content reaches the trimmed edge.
For a normal single plot, start with \documentclass[tikz,border=2pt]{standalone} and leave the axis bounding box intact. Then change the border for outer padding, or change axis settings when the extra space belongs to the plot layout.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.