How to Extend the X-Axis in a PGFPlots Graph
At a glance
- 1. Set the axis limits
Add xmin and xmax to the axis options to choose the visible horizontal range.
- 2. Check the plot domain
If you want the curve to continue into the added range, extend its domain too.
- 3. Choose tick marks
Set xtick or xtick distance if the new range needs specific tick positions.
- 4. Compile and inspect
Check that the limits include your data and that the axis line appears where you expect.
In PGFPlots, the visible horizontal range is controlled by the axis limits. Set xmin and xmax in the axis options to make the graph show a wider x-range. If you also want a plotted function to continue across that newly visible range, extend the function’s domain as well: the axis limits and plot domain control different things.
Set the visible x-range
The xmin and xmax keys specify the left and right ends of the displayed x-axis. Here is a complete example that shows an x-axis from 0 to 8, although the curve is plotted only from 0 to :
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=8,
ymin=-1.5,
ymax=1.5,
axis lines=middle,
xlabel={$x$},
ylabel={$y$},
xtick distance=1,
]
\addplot[blue, thick, domain=0:6.283, samples=100]
{sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
The horizontal axis reaches , but the sine curve stops near . That is because xmax=8 sets the viewing range, while domain=0:6.283 sets where PGFPlots evaluates this function. To draw the curve all the way to the right edge, change the plot option to domain=0:8.
The example uses axis lines=middle to place the axes through the origin. If you want the x-axis along the bottom edge of the plot instead, use axis lines=left or set axis x line=bottom according to the axis layout you need. The xmin and xmax settings still determine its horizontal extent.
Pick limits or automatic padding
Use explicit limits when you know the exact interval you want. For example, xmin=-2, xmax=10 fixes the displayed range from to . This is usually the clearest way to extend an axis to a particular value.
If you only want some extra space around automatically determined data limits, use enlarge x limits instead. For example:
\begin{axis}[
enlarge x limits=0.1,
]
A value such as 0.1 requests relative padding based on the data range. To add a fixed amount of padding, use an absolute value, for example enlarge x limits={abs=1}. This adds padding on the ends rather than setting a specific endpoint. If you need the axis to end at exactly 10, prefer xmax=10.
Set ticks on the extended axis
PGFPlots chooses tick positions automatically by default. If the wider range produces inconvenient labels or spacing, specify them explicitly. For regularly spaced ticks, use xtick distance=2 to place them every two units. For particular positions, use a list such as xtick={0,2,4,6,8}.
Ticks are separate from the axis limits: adding a tick at 8 does not itself extend the axis to 8. Set xmax=8 to establish the endpoint, then choose ticks as needed.
Common mistakes
- Changing
domainbut not the axis limits: The function may be evaluated at larger x-values, but those values can remain outside the displayed range. Increasexmaxas well. - Changing the axis limit but not the domain: The axis gets longer, but the curve may stop before reaching the new endpoint. Extend
domainif the function should continue. - Using padding when you need a precise endpoint:
enlarge x limitsis intended to add space around data; usexminandxmaxfor exact boundaries. - Confusing the axis line with the plotted curve: Extending the visible axis does not create new data or add points to a plot. It only changes the coordinate window shown.
- Forgetting that limits crop the view: Data outside
xminandxmaxare not normally visible in the axis. Set limits that include every point or curve segment you intend to show.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.