4

How can I clip the paths of a 3D plot to the 3D axis bounding box? I am not talking about clipping the path to the projection of the bounding box (which pgfplots does by default), I want to remove points that lie outside the box in 3D.

Consider the following plot:

\documentclass{standalone}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat = 1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin = -1.1, xmax = 1.1,
             ymin = -1.1, ymax = 1.1,
             zmin = -1.1, zmax = 1.1]
\addplot3[domain = -1:1,samples = 20,
          samples y = 0, very thick] (0, 0, {2 * x});
\end{axis}
\end{tikzpicture}
\end{document}

Not clipped

What I want (without changing the plot formula), is this:

Correctly clipped

Note that markers are clipped to the box: change \addplot3 to \addplot3+, and markers will appear on the part inside the box, but not on the part outside.

Markers are clipped

I tried various clip options from the manual, but so far nothing I tried was successful.

3

You can use the restrict z to domain key. It can be applied for the entire axis or per-plot, whichever you require.

\documentclass{standalone}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat = 1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin = -1.1, xmax = 1.1,
             ymin = -1.1, ymax = 1.1,
             zmin = -1.1, zmax = 1.1]
\addplot3[domain = -1:1,samples = 20,
          samples y = 0, very thick,
          restrict z to domain={-1.1:1.1}] (0, 0, {2 * x});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Great! Now that I know what it is called, a note to future readers: restrict x to domain and restrict y to domain also exist. – Ruud Jun 26 '15 at 22:30

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.