How to Set the Sampling Step Size in PGFPlots
At a glance
- 1. Choose the domain
Set the interval endpoints with the domain key.
- 2. Count the intervals
Divide the domain length by your desired step size.
- 3. Set the sample count
Use one more sample than the number of intervals.
- 4. Check the spacing
Confirm the endpoints and sample count give the intended spacing.
To control the distance between evaluation points in a PGFPlots function plot, set the domain and calculate the number of samples. PGFPlots distributes the samples evenly across the domain, including both endpoints. There is no direct step option for this kind of function sampling.
Calculate the sample count
Suppose the domain runs from to , and you want samples separated by a step size . The number of intervals is . Since a sequence of intervals has endpoints, use:
The result must be an integer if both domain endpoints are to be included with exactly that spacing. Equivalently, PGFPlots places samples at spacing when samples=N.
Worked example: spacing of 0.25
To plot from to at intervals of , the domain length is . This gives intervals, so use samples.
Here is a complete, minimal example that compiles with LaTeX and the pgfplots package:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
grid=major
]
\addplot[domain=-1:1, samples=9, mark=*] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
The nine evaluation points are . The mark=* option displays those points, making the sampling positions visible. Remove it if you want only the connected curve.
For another example, to sample the interval from to every , use domain=0:2, samples=21: there are 20 intervals and 21 endpoint-inclusive samples.
What the samples do
For a function such as {x^2}, PGFPlots evaluates the expression at the selected x-values and connects the resulting points. The samples key therefore affects how finely the plotted curve is approximated; it does not set the axis tick spacing. Use xtick or related tick options to control where axis labels and tick marks appear.
A smaller step generally gives a smoother-looking curve, but also creates more points. For straight lines, extra samples are usually unnecessary. For curves with rapid changes or high curvature, you may need a finer spacing in the relevant region.
When the spacing does not divide the domain
If is not an integer, you cannot have all three of these at once: both endpoints included, every interval exactly , and a uniform spacing. For example, a domain of length cannot be divided into intervals of while landing exactly on both endpoints.
Choose which condition matters most. You could adjust the upper endpoint, choose a step that divides the domain length, or use an explicit set of x-coordinates if the final interval may be shorter. For explicitly specified points, PGFPlots supports the samples at option; alternatively, plot supplied coordinate or table data when those are the values you need to preserve.
Common mistakes
- Using the number of intervals as
samples:samplescounts points, not gaps. For equal intervals, use samples. - Expecting
samplesto control tick marks: It controls function evaluation points. Axis ticks are configured separately. - Choosing a non-integer sample count:
samplesmust be an integer. Check that the requested step divides the domain length before applying the formula. - Assuming the curve is evaluated at every possible x-value: PGFPlots evaluates a finite set of points and connects them. A very coarse sampling can miss features between those points.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.