5

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.
            \addplot3 [color=red,solid] table {
0   0.5 0
0   0   1
0.5 0   0
};
            \addplot3 [color=red,solid] table {
0   0   0
0.5 0   1
1   0   0
            };
            \addplot3 [color=red,solid] table {
0.5 0   0
1   0   1
1   0.5 0
};
            \addplot3 [color=red,solid] table {
0   1   0
0   0.5 1
0   0   0
};
            \addplot3 [color=red,solid] table {
1   0   0
1   0.5 1
1   1   0
};
            \addplot3 [color=red,solid] table {
0.5 1   0
0   1   1
0   0.5 0
};
            \addplot3 [color=red,solid] table {
1   1   0
0.5 1   1
0   1   0
};
            \addplot3 [color=red,solid] table {
1   0.5 0
1   1   1
0.5 1   0
};

            % Plot function on the interior.
            \addplot3
            [
                surf,
                mesh/rows=10,
                blue,
                faceted color=black,
            ]
            table[header=false] {3ddata.dat};

        \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
  • 3
    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
5

The section 4.6.1, of pgfplots manual (v1.11) has this about z buffer:

pgfplots supports z buffering techniques up to a certain extend: it works pretty well for single scatter plots (z buffer=sort), mesh or surface plots (z buffer=auto) or parametric mesh and surface plots (z buffer=sort). However, it cannot combine different \addplot commands, those will be drawn in the order of appearance. You may encounter the limitations sometimes. Maybe it will be improved in future versions.

If you decide that you need high complexity, speed and 100% reliable z buffers (depth information), you should consider using other visualization tools and return to pgfplots in several years. If you can wait for a complex picture and you do not even see the limitations arising from z buffering limitations, you should use pgfplots.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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