# PGFplots z buffer does not sort the plotted objects properly

I have a PGFplots axis environment in which I attempt to plot several line objects and a surface object.

The objects have to be 'sorted' in the sense of which object is drawn before or behind the other.

The z buffer key from PGFplots is supposed to resolve this, but I cannot seem to get it to work properly.

Depending on the order of the objects in the script, the surface object makes the line objects dissappear or the line objects are just shown on top of the surface object while they're supposed to be behind.

How do I solve this?

Minimum working example:

\documentclass{standalone}

\usepackage{filecontents}

\begin{filecontents}{3ddata.dat}
0   0   0
0.125   0   0
0.25    0   0
0.375   0   0
0.5 0   0
0.5 0   0
0.625   0   0
0.75    0   0
0.875   0   0
1   0   0
0   0.125   0.25
0.125   0.125   0.1875
0.25    0.125   0.125
0.375   0.125   0.0625
0.5 0.125   0
0.5 0.125   0
0.625   0.125   0
0.75    0.125   0
0.875   0.125   0
1   0.125   0
0   0.25    0.5
0.125   0.25    0.375
0.25    0.25    0.25
0.375   0.25    0.125
0.5 0.25    0
0.5 0.25    0
0.625   0.25    0
0.75    0.25    0
0.875   0.25    0
1   0.25    0
0   0.375   0.75
0.125   0.375   0.5625
0.25    0.375   0.375
0.375   0.375   0.1875
0.5 0.375   0
0.5 0.375   0
0.625   0.375   0
0.75    0.375   0
0.875   0.375   0
1   0.375   0
0   0.5 1
0.125   0.5 0.75
0.25    0.5 0.5
0.375   0.5 0.25
0.5 0.5 0
0.5 0.5 0
0.625   0.5 0
0.75    0.5 0
0.875   0.5 0
1   0.5 0
0   0.5 1
0.125   0.5 0.75
0.25    0.5 0.5
0.375   0.5 0.25
0.5 0.5 0
0.5 0.5 0
0.625   0.5 0
0.75    0.5 0
0.875   0.5 0
1   0.5 0
0   0.625   0.75
0.125   0.625   0.5625
0.25    0.625   0.375
0.375   0.625   0.1875
0.5 0.625   0
0.5 0.625   0
0.625   0.625   0
0.75    0.625   0
0.875   0.625   0
1   0.625   0
0   0.75    0.5
0.125   0.75    0.375
0.25    0.75    0.25
0.375   0.75    0.125
0.5 0.75    0
0.5 0.75    0
0.625   0.75    0
0.75    0.75    0
0.875   0.75    0
1   0.75    0
0   0.875   0.25
0.125   0.875   0.1875
0.25    0.875   0.125
0.375   0.875   0.0625
0.5 0.875   0
0.5 0.875   0
0.625   0.875   0
0.75    0.875   0
0.875   0.875   0
1   0.875   0
0   1   0
0.125   1   0
0.25    1   0
0.375   1   0
0.5 1   0
0.5 1   0
0.625   1   0
0.75    1   0
0.875   1   0
1   1   0
\end{filecontents}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}

% Plot the function cuts at the boundary.
0   0.5 0
0   0   1
0.5 0   0
};
0   0   0
0.5 0   1
1   0   0
};
0.5 0   0
1   0   1
1   0.5 0
};
0   1   0
0   0.5 1
0   0   0
};
1   0   0
1   0.5 1
1   1   0
};
0.5 1   0
0   1   1
0   0.5 0
};
1   1   0
0.5 1   1
0   1   0
};
1   0.5 0
1   1   1
0.5 1   0
};

% Plot function on the interior.
[
surf,
mesh/rows=10,
blue,
faceted color=black,
]

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


The resulting picture looks as follows:

• Like this: i.stack.imgur.com/VcYaI.png ? – user11232 Feb 13 '15 at 10:47
• Z buffer can not combine different \addplot commands and they are in the order of appearance. – user11232 Feb 13 '15 at 10:50
• @HarishKumar No, not like that, since there are red lines that show 'through' the surface plot, even though they should be 'behind' the surface plot. – Adriaan Feb 13 '15 at 17:01
• Your example compiles only if you remove row sep=crcr. Aside from that, it looks fine to me (in pgfplots 1.11 and 1.12). What is the outcome of the example shown in the question? – Christian Feuersänger Feb 13 '15 at 18:46
• @ChristianFeuersänger My example also compiles with row sep=crcr without a problem. Anyways, I removed row sep=crcr, denoted \pgfplotsset{compat=newest} in the preamble. In this case, the outcome of the MWE is as depicted in the answer. I just put the picture there. – Adriaan Feb 16 '15 at 10:32

The section 4.6.1, of pgfplots manual (v1.11) has this about z buffer:
In your case, since you are using different \addplot commands to plot, your efforts to change the order of appearance won't work. You should either try to use a single \addplot command (if possible) or try some other tool to plot the graph.