[Tex/LaTex] How to make a Bezier curve with control points in pgfplots

pgfplots

I would like to create a Bezier curve with control points. I'm having no success finding what I want in the pgfplots manual. I was hoping someone could help me.

What I would like to do is create a curve by joining piecewise pieces defined using Bezier curves and control points. This is to create graphs for calculus students where I give them the graph of a function and they have to create a graph of the derivative. So, I would like to be able to precisely control where inflection points, extrema, etc….

Here's an example, but I have no clue what's going on or why the points have been connected in the order that they've been connected in.

\documentclass[border=6pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)},
title={\texttt{patch type=cubic spline}}]
\addplot[
        mark=*,
        patch,
        patch type=cubic spline]
        coordinates {
        (2,2)        [0]
        (0,2)        [0]
        (2,0)        [0]
        (0,0)        [1]
};
\end{axis}
\end{tikzpicture}

\end{document}

My understanding of what I read was that the tags [0] would help define control points. I expected this curve to look something like an S shape starting at (2,2) and ending at (0,0) where (0,2) and (2,0) were control points.

I thought I could accomplish this with something like:

\documentclass[border=6pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
    [nodes near coords={(\coordindex)},
     title={\texttt{patch type=cubic spline}}
    ]

   \draw (axis cs:2,2) .. controls (axis cs:0,2) and (axis cs:2,0) .. (axis cs:0,0);

\end{axis}

\end{tikzpicture}

\end{document}

But pgfplots seems to ignore my path for its bounding box.

Best Answer

The feature patch type=cubic spline expects interpolation points, i.e. points which are on the curve. It chooses the unique cubic spline which passes through (= interpolates) the four points.

\draw ... controls <A> and <B> .. is the TikZ instruction for bezier drawing operations and does what you expect it to do.

In pgfplots, only coordinates inside of an \addplot ...; contribute to axis limits.

It sounds as if you two choices:

  1. to use an interpolatory basis together with pgfplots or
  2. to rely on a pure tikz solution with \draw .. controls .. (which can be concatenated, by the way).

Solution 2. can be drawn inside of a pgfplots axis; in this case, axis limits need to be given using xmin=-1,xmax=3,ymin=-1,ymax=3 or something like that:

\documentclass[border=6pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}

% 1.11 does not need "axis cs:", i.e. (axis cs:2,2) is equivalent to (2,2)
\pgfplotsset{compat=1.11}
\begin{document}

\begin{tikzpicture}
\begin{axis}
    [
    title={\texttt{patch type=cubic spline}},
    xmin=-1,xmax=3,ymin=-1,ymax=3,
    ]


   \draw (2,2) .. controls (0,2) and (2,0) .. (0,0);

    \node at (2,2) {$0$};
    \node at (0,2) {$1$};
    \node at (2,0) {$2$};
    \node at (0,0) {$3$};

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here