6

I just tried to make a 3D parametric plot of the spiraling helix r(t) = {t, cos(2*pi*t), sin(2*pi*t)} using PGFPlots, but there seems to be an extra line connecting the points r(t_min) and r(t_max). What is causing this?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8} %Fix axis labels in 3D when axis lines=center

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            axis lines=center,
            width=800bp,
            xmin=-8,xmax=8,
            ymin=-8,ymax=8,
            zmin=-8,zmax=8,
            xlabel={$x$},
            ylabel={$y$},
            zlabel={$z$},
            view={60}{60},
            smooth
            ]
        \addplot3[
                blue,samples=60,
                domain=-4:4,
                ]
                ({x},{x*cos(deg(2*pi*x))},{x*sin(deg(2*pi*x))});    
    \end{axis}
\end{tikzpicture}

\end{document}

Here's the pgfplots output (with extra line): PGFPlots Spiral

Here's the same function plotted in Mathematica: Mathematica Spiral

9

Hah, you have to read the fine print in the pgfplots manual (Chapter 4.6.2 or so):

Furthermore, \addplot3 has a way to decide whether a line visualization or a mesh visualization has to be done. The first one is a map from one dimension into R^3 and the latter one a map from two dimensions to R^3. Here, the keys mesh/rows and mesh/cols are used to define mesh sizes (matrix sizes). Usually, you don’t have to care about that because the coordinate input routines already allow either one- or two-dimensional structure.

Apparently, in your case, \addplot3 gets it wrong and tries to place a mesh on this curve. This leads to a couple of additional lines. A few pages after, the documentation specifies that you have to change the key y domain:

If y domain is empty, [y1,y2 ] = [x1,x2] will be assumed. If y domain=0:0 (or any other interval of length zero), it is assumed that the plot does not depend on y (thus, it is a line plot).

So the correct command is:

\addplot3[
            y domain=0:0,
            blue,samples=60,
            domain=-4:4,
            ]
            ({x},{x*cos(deg(2*pi*x))},{x*sin(deg(2*pi*x))});
|improve this answer|||||
  • 3
    Instead of y domain=0:0, you can also set samples y=0 to get the same result. – Jake Oct 14 '15 at 5:34
  • 2
    I used \pgfplots{parametric nonclosed/.style={samples y=0}} to get this effect semantically. – Matthew Leingang Sep 16 '16 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.