# tikz/pgfplots - Plotting 3D surface with cloud of points

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}

\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\\
};

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

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


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

\documentclass[tikz]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\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\\
};

surf,
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}


• Thank you for the answer! It works perfectly on the simple example that I showed, but not on the case where I need it. If I plot on different layers, it'll move all the points in front or behind the surface. I guess this can be solved by dividing the points into two groups, in front and behind surface, before plotting. This is, however, not always a simple task. Is there any way to tell the software to make this distinction on its own? I’ve updated the coordinates of the points in the original question to reflect the core of the problem better. – Tobias Friis Jan 15 '20 at 12:22

This is an extension of erahfeza's answer that installs filters. The filters here checks whether the dot is above or below the surface. (It is not absolutely perfect because the dot could be "behind" the surface at large y. I assume that you do not have such points. If you do, one needs to take this possibility into account. Then the filter will depend on the view angle.)

\documentclass[tikz]{standalone}

\usepackage{amsmath}
\usepackage{pgfplots}
\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},mark layer=like plot]

\addplot3 [color=green, draw=none, mark=*, mark size=2,
on layer=background,
z filter/.expression={z<x^2-y^2 ? z : nan}]
table[row sep=crcr] {%
0 0 15\\
0 0 -15\\
};

\addplot3 [color=green, draw=none, mark=*, mark size=2,
on layer=foreground,
z filter/.expression={z>x^2-y^2 ? z : nan}]
table[row sep=crcr] {%
0 0 15\\
0 0 -15\\
};