# pgfplots piece-wise defined path not aligning

I was trying to imitate a question (Drawing closed 3d path with pgfplots) and do a piecewise-defined curve in pgfplots, but my plots are not aligning on the z-axis, despite the math (assuming I didn't flub my calculations) saying they should. Changing the multiple of pi in the second addplot3 doesn't usually change it unless I do a large multiple like 25 or 200. Making them into separate \begin{axis}...\end{axis} environments doesn't make it work either.

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

\begin{document}
$\begin{tikzpicture} \begin{axis}[axis lines=middle,view={135}{45},xlabel=x,ylabel=y,zlabel=z,clip=false,xtick=\empty,ytick=\empty,ztick=\empty] \addplot3[double,domain=0:180,samples=90,samples y=1] ({1-cos(2*x)},{-sin(2*x)},{0.125*sin(4*x)-0.5*x}); \addplot3[double,domain=180:360,samples=90,samples y=1] ({cos(2*x)-1},{-sin(2*x)},{0.5*x-0.125*sin(4*x)-pi}); \end{axis} \end{tikzpicture}$
\end{document}


Is there an easy way of making the plots "align" properly (the top loop should be shifted downwards with respect to the z axis so that it forms a closed loop) that I am overlooking? The z-axis also looks to be uneven unlike the x and y so I don't know if that is a symptom of whatever is going on. Thanks for any insight!

EDIT: with a very good catch from KersouMan (thank you!!), it appears I mixed the pi in with degrees, so editing for that (using 180 instead of pi), I get a loop as desired, but it is significantly more wacky than it should be. Pictured below:

The graph should look more like a very plain figure 8 (composed of two kissing circles) if you project to the x,y-plane.

How it should look (or sort of, at least) is like this:

EDIT 2: Here is some better code to replicate the third image above:

        $\begin{tikzpicture} \begin{axis}[axis lines=middle,view={135}{45},xlabel=x, ylabel=y,zlabel=z,clip=false,xtick=\empty,ytick=\empty,ztick=\empty,zmax=120] \addplot3[double,domain=0:180,samples=90,samples y=1] ({1-cos(2*x)},{-sin(2*x)},{0.125*sin(4*x)-0.5*x}); \addplot3[double,domain=180:360,samples=90,samples y=1] ({cos(2*x)-1},{-sin(2*x)},{0.5*x-0.125*sin(4*x)-180}); \end{axis} \end{tikzpicture}$


I feel like there is a little distortion here and there (maybe it is the viewing angle?), but otherwise, decent enough. I am glad it is at least now a closed loop!

• Could you please explain in more detail what align mean? I.e. in which way does the output not coincide with your expectation. (BTW, it is appreciated if you add a complete document that starts with \documentclass and ends with \end{document}.) – user121799 Mar 27 '19 at 15:05
• If I understand well what you want to plot, I think the problem is in your formula for the second curve. By using ({cos(2*x)-1},{sin(2*x)},{0.5*x-0.125*sin(4*x)-0.5*180}), I obtain a continuous line (and do not use pi in your formulas if the domain is set in degrees). It is even simpler if you use ({cos(2*x)-1},{sin(2*x)},{0.5*x-0.125*sin(4*x)}) with domain=0:180 as the second plot. – KersouMan Mar 27 '19 at 15:06
• @marmot I have edited accordingly – anakhro Mar 27 '19 at 15:19
• @KersouMan thank you!! That fixes one problem, but another problem now presents itself: the z-axis is all messed up, and the loop still doesn't quite look like it should! I will try to edit a picture to show how I wish it would look. See the edit for how it looks now. – anakhro Mar 27 '19 at 15:20
• You can add some appropriate zmax to the axis options. – user121799 Mar 27 '19 at 15:25

This is a proposal to draw the curve in one plot. Depending on what you want to achieve, you may be better off with trig format=rad, which allows you to specify the angles in radians. I do not have the full picture of what you are after.

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

\begin{document}
$\begin{tikzpicture} \begin{axis}[axis lines=middle,view={135}{45},xlabel=x, ylabel=y,zlabel=z,clip=false,xtick=\empty,ytick=\empty,ztick=\empty,zmax=120] \addplot3[double,domain=0:360,samples=181,samples y=1] ({sign(180-x)*(1-cos(2*x))},{-sin(2*x)}, {ifthenelse(x<180,0.125*sin(4*x)-0.5*x,0.5*x-0.125*sin(4*x)-180)}); \end{axis} \end{tikzpicture}$
\end{document}


• Nice! I do like the overlap of the original, however, just to give it that 3D element. – anakhro Mar 27 '19 at 17:02