# Colormap drawing order in parametric 3D tube

Similar to this question, I am trying to draw a parametric tube in a PGFplots 3D-plot:

\documentclass{article}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}
surf,
samples=2,
samples y=20,
y domain=0:360,
colormap/blackwhite
]
({sin(y)}, {x}, {cos(y)});
\end{axis}
\end{tikzpicture}
\end{document}


yielding:

I am not sure, how to fix the drawing order of the colormap. It seems quite glitchy as-is.

Putting the domain so that it joins at the bottom, where in the current view there is no overlap of the tube with itself, fixes it (y domain=-70:290):

The issue with that is of course, that once the current view changes, we have to manually adjust the y domain.

How can the colormap overlaying itself be fixed?

pgfplots is very smart but sometimes one needs to help it a bit. Here you need z buffer=sort.

\documentclass{article}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}
surf,
samples=2,
samples y=20,
y domain=0:360,
colormap/blackwhite,z buffer=sort
]
({sin(y)}, {x}, {cos(y)});
\end{axis}
\end{tikzpicture}
\end{document}


I was not planning on answering this question myself, but I found the key z buffer in the PGFplots manual, in Section 4.6.5. Mesh Plots.

Supplying it gives

\documentclass{article}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}
surf,
samples=2,
samples y=20,
y domain=0:360,
colormap/blackwhite,
mesh/interior colormap name=hot,
z buffer=sort
]
({sin(y)}, {x}, {cos(y)});
\end{axis}
\end{tikzpicture}
\end{document}


which works and also allows correct plotting of an interior colormap for the mesh: