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.

1
  • 1
    More info here and here. It is not very easy, as I remember.
    – Roald
    Sep 27, 2016 at 20:43

1 Answer 1

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

3
  • Ok, yes, in this case it helps. But my plot is not that simple. This is just a boiled down example...
    – Rob
    Sep 27, 2016 at 5:56
  • I guessed as much. I've found that true 3D rendering is quite hard with pgfplots.
    – Derek
    Sep 27, 2016 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. ;-)
    – user121799
    Aug 21, 2018 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .