2

In this and this question, we learned how to draw real line plots in 3D axes in PGFPlots by supplying samples y=1 (samples y=0 seems to work as well) or y domain=0:0 to get rid of meshes/matrices.

However, I could not get it to work for the 'perpendicular' direction, when we try to plot a line plot in the X-Y-plane (where x=const., as opposed to the X-Z-plane (where y=const.). The result was not a line plot, but a 'fake' 3D plot, with the colormap still intact:

%!TEX TS-program = lualatex
\documentclass{article}

\usepackage{pgfplots}
    \pgfplotsset{compat=newest}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xlabel={x},
            ylabel={y},
            zlabel={z},
            domain=0:5*360,
            y domain=0:5*360,
            surf
            ]
            % These work:
            \addplot3[ultra thick, samples y=1] ({x}, {0}, {cos(x)});
            \addplot3[ultra thick, samples y=1] ({x}, {0}, {cos(x)+4});

            % This is in analogy to above, but does not work
            % (Package pgfplots Warning: the current plot has no coordinates (or all have been filtered away)). 
            \addplot3[ultra thick, samples=1] ({5*360}, {y}, {cos(y)+2});

            % These work, but don't give a line plot:
            \addplot3[ultra thick, domain=5*360:5*360] ({x}, {y}, {cos(y)+2});
            \addplot3[ultra thick] ({5*360}, {y}, {cos(y)+2});
        \end{axis}
    \end{tikzpicture}
\end{document}

yielding:

broken 3d line plot

I do not know why \addplot3[ultra thick, samples=1] ({5*360}, {y}, {cos(y)+2}); does not work. How can we produce real line plots in the second direction as well?

4

This is somewhat similar to your own answer but arguably from a different perspective. You are considering parametric plots, in which x and y are just parameters or placeholders, and not to be confused with the x and y coordinates. To make this a bit clearer, I rename the parameter t. Effectively this is then practically the same as your answer (with surf removed and the plots made smooth). I also added a plot that does not go along any of the axes to reiterate the point.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xlabel={$x$},
            ylabel={$y$},
            zlabel={$z$},
            domain=0:5*360,
            y domain=0:5*360,
            samples=101,smooth
            ]
            % These work:
            \addplot3[ultra thick,variable=\t, samples y=0] ({t}, {0}, {cos(t)});
            \addplot3[ultra thick,variable=\t, samples y=0] ({t}, {0}, {cos(t)+4});
            \addplot3[ultra thick,variable=\t, samples y=0] ({5*360}, {t}, {cos(t)+2});
            \addplot3[ultra thick,variable=\t, samples y=0,color=blue] 
            ({900+800*cos(t)}, {900+800*sin(t)}, {cos(12*t)+1});
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • 1
    I was wondering why the line plot elements weren't joined at their ends. Omitting surf for those plots solved that, nice. And also yes, parametric plots confuse me a lot when we throw around xs and ys so much. Thanks for your answer. – Hans Lollo Jul 12 at 10:11
2

If we swap around the parametric functions we can rely on the working y samples=0 approach like so:

%!TEX TS-program = lualatex
\documentclass{article}

\usepackage{pgfplots}
    \pgfplotsset{compat=newest}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xlabel={x},
            ylabel={y},
            zlabel={z},
            domain=0:5*360,
            y domain=0:5*360,
            surf
            ]
            % These work:
            \addplot3[ultra thick, samples y=1] ({x}, {0}, {cos(x)});
            \addplot3[ultra thick, samples y=1] ({x}, {0}, {cos(x)+4});

            % Swapping around the parametric functions allows it to work:
            \addplot3[ultra thick, samples y=1] ({5*360}, {x}, {cos(x)+2});
        \end{axis}
    \end{tikzpicture}
\end{document}

yielding:

line plot in 3d

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.