I'm trying to draw some smooth looking surfaces with gridlines, and I'd like the grid lines to be invisible (or transparent or whatever) when they are obscured by my surface. In my code below, you'll see that I manually added the gridlines, because otherwise they seemed to become piece-wise linear.

surface

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=80]
  \addplot3[surf, domain=-3:3, shader=interp] {0.1*(x^3-y^3)};
  \foreach \xx in {-3,-2.8,...,3}
  {
    \addplot3+[domain=-3:3, line width=0.05mm, mark=none, color=black, solid, samples y=0]
     ({\xx}, {x}, {0.1*\xx*\xx*\xx-0.1*x^3});
  }

  \foreach \yy in {-3,-2.8,...,3}
  {
     \addplot3[domain=-3:3, line width=0.05mm, mark=none, solid, color=black, samples y=0]
     ({x}, {\yy}, {0.1*x^3-0.1*\yy*\yy*\yy});
  }
  \end{axis}
\end{tikzpicture}
\end{document}

How do I hide the lines behind the surface in a "smart way"?

  • 1
    Welcome to TeX.SE! How about adding mesh and decreasing the samples? Like \addplot3[samples=30,surf, domain=-3:3,mesh/ordering=y varies] {0.1*(x^3-y^3)}; ? – marmot Aug 14 at 0:12
up vote 9 down vote accepted

I'd just do

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}
  \addplot3[samples=30,surf,shader=faceted interp, domain=-3:3,mesh/ordering=y varies] {0.1*(x^3-y^3)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Whether or no you use shader=faceted interp is not too essential here. You'd need to use spy glasses to notice a difference.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{spy}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[spy using outlines={circle, size=2cm, connect spies,
    every spy on node/.append style={thin}}]
  \begin{axis}[colormap/viridis,width=0.45\linewidth,title=\texttt{shader=faceted}]
  \addplot3[samples=30,surf,shader=faceted,domain=-3:3,mesh/ordering=y varies] {0.1*(x^3-y^3)};
  \end{axis}
  \spy [red,magnification=5] on (3.1,1.8) in node (zoom) [below] at (4.1,0);
\end{tikzpicture}~
\begin{tikzpicture}[spy using outlines={circle, size=2cm, connect spies,
    every spy on node/.append style={thin}}]
  \begin{axis}[colormap/viridis,width=0.45\linewidth,title=\texttt{shader=faceted interp}]
  \addplot3[samples=30,surf,shader=faceted interp,domain=-3:3,mesh/ordering=y varies] {0.1*(x^3-y^3)};
  \end{axis}
  \spy [red,magnification=5] on (3.1,1.8) in node (zoom) [below] at (0,0);
\end{tikzpicture}
\end{document}

enter image description here

enter image description here

If you look very closely, you'll note that the tiles on the right have some gradient shading whereas the ones on the left don't.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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