12

In the pgfplots manual it says:

\addplot+[smooth]
Smooth plots interpolate smoothly between successive points.

In order to choose the right sampling of points from a function evaluation for plotting, I need to know with some detail what is the "interpolation" that "smooth plot" actually pgfplots (or tikz) implements.

For example, is it cubic splines (probably not), Akima splines, home brew? If it is not a standard one, what properties does it have? continuity, continuity of first derivative, second derivative, monotonicity, maximum tolerated curvature?

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
alfC
  • 14,350
  • 1
    I wonder if this is the same "smooth" as the option in TikZ, e.g. \draw plot[id=sin,smooth] function{sin(x)};. – N.N. Jul 08 '11 at 10:34
  • 2
    @N.N.: It is. PGFplots uses the TikZ \draw plot function to do the actual plotting. – Jake Jul 08 '11 at 10:44
  • Thanks, in that case: What is the “smooth” plot interpolation algorithm in tikz? – alfC Jul 08 '11 at 10:48
  • 2
    @alfC: It uses cubic bézier curves to connect the points of your plot, with the two supports for each point calculated using a relatively simple algorithm that takes the previous and next point into account. Do you want more detail on the algorithm used for determining the supports, or do you already know that this makes TikZ' plotting capabilities unsuitable for you purpose? – Jake Jul 08 '11 at 11:08
  • 1
    @Jake: I think that is enough information for the complexity of the sampling algorithm I wanted to implement. I think that given the cubic-bezier then there is no simple best adaptive sampling that I can implement quickly. For linear interpolation (i.e. no-smooth) the optimal sampling is given by recursive trapezoidal but for cubic bezier I guess recursive simpson will do at least part of the job. The idea was to plot functions with minimal evaluation points and pdf size the goal being a good resulting "visual plot". – alfC Jul 08 '11 at 22:13
  • @Jake: Please turn your comment into an answer. – lockstep Jul 14 '11 at 18:39

1 Answers1

9

It uses cubic Bézier curves to connect the points of your plot, with the two supports for each point calculated using a relatively simple algorithm that takes the previous and next point into account.

The support points for the Bézier curves are calculated so that the tangent through one point is is parallel to a line connecting the previous and the next point:

In order to determine the control points of the curve at the point y, the handler computes the vector zx and scales it by the tension factor. Let us call the resulting vector s. Then y + s and ys will be the control points around y. The first control point at the beginning of the curve will be the beginning itself, once more; likewise the last control point is the end itself.

The same approach is taken with smooth cycle.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing, shapes.misc}
\tikzset{show curve controls/.style={decoration={show path construction,curveto code={
  \draw[blue,dashed](\tikzinputsegmentfirst)--(\tikzinputsegmentsupporta)node[at end,cross out,draw,solid,red,inner sep=2pt]{};
  \draw[blue,dashed](\tikzinputsegmentsupportb)--(\tikzinputsegmentlast)node[at start,cross out,draw,solid,red,inner sep=2pt]{};}},decorate}}
\begin{document}
\tikz[mark=*, mark size=1pt]
  \foreach[count=\j] \cycle in {, \space cycle}
    \foreach[count=\i] \tension in {.5, 1, 1.5, 2}
      \draw[shift={(\j*5cm,-\i*2cm)}, postaction=show curve controls]
        plot[tension=\tension, smooth\cycle] coordinates {(0,0)(1,1)(2,.5)(3,1)}
          node[above] {\tension}
          (0,0) edge[densely dotted] (2,.5)
          (1,1) edge[densely dotted] (3,1);
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
Jake
  • 232,450
  • yeah, thanks. I see it can also produce curvature discontinuities in some extreme cases; I guess that is the cost of using a local algorithm. – alfC Jul 15 '11 at 04:02