# \addplot3 in pgfplots plots data beyond axis limits

When using pgfplots for a 3D graph and reading the data from a table, is there a way to truncate the data at the axis limits specified in the axis environment ?

Here is a MWE:

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=16cm,height=8cm,
view={-45}{65},
scale only axis,
xmin=200,xmax=500,
xlabel={data1},
ymin=0,ymax=80,
ylabel={data2},
zmin=0,zmax=1,
zlabel={data3},
]
250 0 0\\
250 25 0.2\\
250 50 0.3\\
250 75 0.2\\
250 100 0\\
};
450 0 0.5\\
450 25 0.1\\
450 50 0\\
450 75 0.1\\
450 100 0.5\\
};
\end{axis}
\end{tikzpicture}%
\end{document}


In this example, the ydata of each table goes up to 100, while the ymax value specified in the axis options is 80. In this case, the output looks like:

Note how the plotted lines go beyond the prescribed axis limits. Is there a way either to read the data up to a certain value (in this case ymax) or an option that can be specified in the axis environment to avoid displaying curves beyond axis limits ?

• You can set restrict y to domain=0:80, but that will only filter out the points that exceed the axis limits, not clip the line at the limiting plane of the axis. Is that what you're trying to achieve? – Jake Sep 4 '14 at 21:42
• @Jake That worked perfectly ! :-) – Nicolas Sep 4 '14 at 21:46

You can filter out the coordinates that fall outside the axis range by setting restrict y to domain=0:80:

\documentclass[tikz, border=5mm]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=16cm,height=8cm,
view={-45}{65},
scale only axis,
xmin=200,xmax=500,
xlabel={data1},
ymin=0,ymax=80, restrict y to domain=0:80,
ylabel={data2},
zmin=0,zmax=1,
zlabel={data3},
]
250 0 0\\
250 25 0.2\\
250 50 0.3\\
250 75 0.2\\
250 100 0\\
};