How to Make the x-Axis Smaller in PGFPlots
At a glance
- 1. Set the plot width
Use the axis width option to make the plot rectangle narrower.
- 2. Set the height
Choose height independently to control the plot's vertical size.
- 3. Control what width means
Add scale only axis when width should apply to the axes, not their labels.
- 4. Check the data limits
Use xmin and xmax to change the displayed data range, not the physical plot size.
To make a PGFPlots x-axis physically shorter, reduce the width of the axis environment. Set height separately to keep the plot as tall as you want. This changes how data coordinates are mapped onto the page; it does not change the data values or the displayed x-range.
A common source of confusion is the difference between shrinking the drawn axis and narrowing the interval of data shown. The options width and height control the plot’s physical size. The options xmin and xmax control its data limits.
Set the plot dimensions
Use width and height as axis options. PGFPlots accepts TeX dimensions such as cm, mm, and pt:
\begin{axis}[
width=3cm,
height=5cm,
]
A smaller width makes the plot rectangle narrower. If the height stays the same, the plot becomes relatively taller and narrower. That is often what you want when only the horizontal scale should be reduced.
The width of the whole figure can be greater than the value you give because tick labels, axis labels, and other decorations may extend beyond the plot rectangle. To make width refer specifically to the axis rectangle, use scale only axis.
A complete example
This example plots the same data over an x-range from 0 to 10, but fits the axis rectangle into a narrow width:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=2.5cm,
height=5cm,
scale only axis,
xmin=0, xmax=10,
ymin=0, ymax=1,
xlabel={$x$},
ylabel={$y$},
axis lines=left,
]
\addplot[blue, thick, domain=0:10, samples=100] {exp(-x/4)};
\end{axis}
\end{tikzpicture}
\end{document}
The width=2.5cm setting makes the axis rectangle 2.5 cm wide, while height=5cm makes it 5 cm tall. The x-values still run from 0 to 10. The curve therefore appears compressed horizontally compared with a plot of the same height and a larger width.
scale only axis excludes axis labels and tick labels from the specified dimensions. Without it, PGFPlots generally accounts for decorations when sizing the complete axis picture, so the resulting figure can be wider than the width value. If your goal is to limit the total width including labels, omit scale only axis and adjust the width until the complete figure fits.
Physical size versus data range
Choose the option that matches what you mean by “smaller”:
| Goal | Use |
|---|---|
| Make the drawn plot narrower | Reduce width |
| Show a smaller interval of x-values | Set xmin and xmax |
| Make the axis rectangle a specified width | Set width and use scale only axis |
| Shrink the entire TikZ picture, including text | Apply a TikZ scaling transformation cautiously |
For example, changing width=8cm to width=3cm compresses the same x-range into less page space. By contrast, changing xmin=0, xmax=10 to xmin=0, xmax=4 displays only the portion of the data with x-values from 0 to 4. It does not, by itself, make the axis rectangle narrower.
If you mean that the numbers on the x-axis are very small, rather than that the axis should be physically short, keep the dimensions appropriate and consider formatting the tick labels or expressing the data in a more convenient unit. Changing width in that case affects the drawing, not the magnitude or notation of the tick values.
Common mistakes
- Changing
xmaxto shrink the drawn axis. This changes the data interval and may crop or rescale the plotted data; usewidthto change the physical dimension. - Setting only
widthwhen you want a particular shape. PGFPlots may choose or retain a height that makes the result look unexpectedly squat or tall. Set both dimensions when the aspect ratio matters. - Expecting the full figure to equal
width. Labels and tick marks can extend beyond the axis rectangle. Usescale only axisif the requested width is for the rectangle itself. - Using a global scale to adjust just the x-direction. A transformation that scales the whole picture also changes text and line widths. Prefer PGFPlots’ axis dimensions when the goal is to lay out the plot at a particular size.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.