1

Here is a simple 3D PGF plot:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3 [color=black,line width=2.0pt]
 table[row sep=crcr] {
0   0   -1\\
0   0   1\\
};
\addplot3 [color=gray,line width=2.0pt]
 table[row sep=crcr] {
1   1   0\\
1  -1   0\\
-1  -1  0\\
-1  1   0\\
1  1    0\\
};
\end{axis}
\end{tikzpicture}
\end{document}

3D plot

The line on the z-axis is supposed to go through the middle of the xy-rectangle. But as you see, the rectangle is plotted on top of the line, since it was plotted last. How to achieve correct overlap in this situation? My actual plot is generated with matlab2tikz, but suffers from the same problem.

Stefan Pinnow
  • 29,535
Rob
  • 319

1 Answers1

3

I don't think this is possible; AFAIK pgfplots isn't truly a 3D rendering engine. A hacky approach is to split the line in two:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot3 [color=black,line width=2.0pt]
    table[row sep=crcr] {
    0   0  -1\\
    0   0   0\\
    };
    \addplot3 [color=gray,line width=2.0pt]
    table[row sep=crcr] {
    1   1   0\\
    1  -1   0\\
    -1  -1  0\\
    -1  1   0\\
    1  1    0\\
    };
    \addplot3 [color=black,line width=2.0pt]
    table[row sep=crcr] {
    0   0   0\\
    0   0   1\\
    };
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Derek
  • 1,748
  • Ok, yes, in this case it helps. But my plot is not that simple. This is just a boiled down example... – Rob Sep 27 '16 at 05:56
  • I guessed as much. I've found that true 3D rendering is quite hard with pgfplots. – Derek Sep 27 '16 at 20:04
  • +1. You may be able to solve it with a patch plot. (Sorry, your answer to my recent question is not upvotable in its present form, so I upvoted this and another answer of yours to show that I do appreciate your effort very much. ;-) –  Aug 21 '18 at 23:07