[Tex/LaTex] tikz/pgfplots – Plotting 3D surface with cloud of points

3dpgfplotstikz-pgf

I'm trying to make a 3D plot with a surface and 1000 points in a cloud, where some of the points will be hidden behind the surface, but slightly visibly due to the transparency of the surface. However, no matter what I do, all the points are entirely visible and the surface is plotted behind the hidden points. In order to illustrate the problem, I’ve made a simple example with two points and a transparent surface. The upper is not hidden by the surface and should be entirely visible as it is. The second point, which is hidden by the surface, should be plotted behind the transparent surface, but is just as visible as the first point. Any suggestions of how to deal with this issue?

\documentclass[tikz]{standalone}

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

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[view={25}{30}]

            \addplot3 [color=green, draw=none, mark=*, mark size=2]
                table[row sep=crcr] {%
                0 -4 10\\
                0 0 -15\\
                };

            \addplot3 [
                surf,
                shader=faceted,
                fill opacity=0.75,
                samples=25,
                domain=-4:4,
                y domain=-4:4
                ] {x^2-y^2};

        \end{axis}
   \end{tikzpicture} 
\end{document}

Best Answer

You should need to plot the \addplots on different layers.

\documentclass[tikz]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{
            layers/my layer set/.define layer set={
            background,
            main,
            foreground
        }{
           },
           set layers=my layer set,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[view={25}{30}]

            \addplot3 [color=green, draw=none, mark=*, mark size=2]
                table[row sep=crcr] {%
                0 0 15\\
                0 0 -15\\
                };

            \addplot3 [
                surf,
                shader=faceted,
                fill opacity=0.75,
                samples=25,
                domain=-4:4,
                y domain=-4:4,
                on layer=foreground,
                ] {x^2-y^2};

        \end{axis}
   \end{tikzpicture} 
\end{document}

enter image description here