Tx · LaTeX & TikZ

How to Draw a Spline with PGFPlots

Published By the 9to5Science editorial team

At a glance

  1. 1. List your points

    Write the data points in the order the curve should pass through them.

  2. 2. Enable smoothing

    Add the smooth option to an addplot command with coordinates.

  3. 3. Check the curve

    Compile and inspect for overshoot; adjust the points or use a true spline calculation if needed.

To draw a smooth curve through data points in PGFPlots, add the smooth option to \addplot. This works well when you want a visually smooth connection between a modest number of coordinates.

The term spline can also mean a specific mathematical interpolant, such as a natural cubic spline. PGFPlots’ smooth option is not a request for that particular spline construction; the distinction matters if the curve must satisfy defined interpolation or boundary conditions.

A minimal working example

This complete example draws a smooth curve through six coordinates and places a marker at each supplied point. Compile it with a LaTeX engine such as pdfLaTeX.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xlabel={$x$},
  ylabel={$y$},
  grid=both,
  xmin=0, xmax=5,
  ymin=0, ymax=4
]
  \addplot+[smooth, thick, mark=*] coordinates {
    (0,0.4)
    (1,1.1)
    (2,2.7)
    (3,2.0)
    (4,3.3)
    (5,2.9)
  };
\end{axis}
\end{tikzpicture}
\end{document}

The coordinates list gives the points the curve passes through. smooth changes how PGFPlots connects them; thick changes the line width, and mark=* marks the original coordinates. The + asks PGFPlots to apply the plot-cycle style first and then these options, so explicit options can override matching cycle-list settings. Without +, the cycle-list style is applied after the options and can override matching settings; it is not simply replaced.

Adjusting the curve

The smooth path is built from curved segments, with the shape influenced by neighboring points. You can change the curve’s tension with the TikZ tension option:

\addplot+[smooth, tension=0.7, mark=*] coordinates {
  (0,0) (1,2) (2,1) (3,3)
};

Try a few values and inspect the result: changing the tension changes the curvature, not the supplied data points. The appropriate value depends on the shape you want. You can also remove mark=* for a clean curve without markers.

For a graph of a function rather than a list of measured points, PGFPlots can sample the function directly. For example:

\addplot[smooth, domain=0:5, samples=100] {sin(deg(x))};

Here samples controls how many function values are evaluated over the domain. Smooth styling changes the way those samples are connected; it does not improve the accuracy of the function evaluations themselves. Increase the sample count if a rapidly changing curve looks angular.

When smooth is not a true cubic spline

A visually smooth curve and a mathematically defined spline are not interchangeable. The smooth option is useful for presentation, but it does not promise a natural cubic spline, monotonic interpolation, or a curve without overshoot. If you need particular spline conditions—for example, zero second derivative at the endpoints—compute the spline using a method that specifies those conditions. Then export enough points along the computed curve and plot those points in PGFPlots, for example with \addplot table.

This distinction is especially important for measured data. A smooth visual curve can suggest values between measurements that are not supported by the data. If the points represent a quantity that must remain monotone or within a physical range, use an interpolation method suited to those constraints rather than relying on visual smoothing.

Common mistakes

  • Forgetting smooth: Without it, PGFPlots connects the coordinates with straight line segments.
  • Expecting a particular spline formula: smooth does not select a natural, clamped, or other named cubic spline.
  • Using points in the wrong order: For a function of xx, list points in increasing xx order. Otherwise the path follows the listed order and may double back or loop.
  • Assuming the curve cannot overshoot: Curved segments can rise above or fall below nearby data values. Check the plot, especially when there are sharp changes or widely spaced points.
  • Confusing markers with extra data: mark=* shows the coordinates you supplied; it does not add interpolation samples.

For a quick, attractive curve through points, smooth is usually the simplest choice. Use a separately calculated spline when the interpolation rule itself is part of the problem.

References

  1. PGFPlots Manual
  2. PGF/TikZ Manual: Plots

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