Tx · LaTeX & TikZ

How to Set the Sampling Step Size in PGFPlots

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the domain

    Set the interval endpoints with the domain key.

  2. 2. Count the intervals

    Divide the domain length by your desired step size.

  3. 3. Set the sample count

    Use one more sample than the number of intervals.

  4. 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 aa to bb, and you want samples separated by a step size hh. The number of intervals is (b−a)/h(b-a)/h. Since a sequence of nn intervals has n+1n+1 endpoints, use:

samples=b−ah+1.\texttt{samples} = \frac{b-a}{h}+1.

The result must be an integer if both domain endpoints are to be included with exactly that spacing. Equivalently, PGFPlots places NN samples at spacing (b−a)/(N−1)(b-a)/(N-1) when samples=N.

Worked example: spacing of 0.25

To plot y=x2y=x^2 from x=−1x=-1 to x=1x=1 at intervals of 0.250.25, the domain length is 22. This gives 2/0.25=82/0.25=8 intervals, so use 8+1=98+1=9 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 −1,−0.75,−0.5,−0.25,0,0.25,0.5,0.75,1-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1. 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 00 to 22 every 0.10.1, 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 (b−a)/h(b-a)/h is not an integer, you cannot have all three of these at once: both endpoints included, every interval exactly hh, and a uniform spacing. For example, a domain of length 11 cannot be divided into intervals of 0.30.3 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: samples counts points, not gaps. For nn equal intervals, use n+1n+1 samples.
  • Expecting samples to control tick marks: It controls function evaluation points. Axis ticks are configured separately.
  • Choosing a non-integer sample count: samples must 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

  1. PGFPlots: 2D and 3D graphics in LaTeX — Package Documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.