# Specifying a domain for the z-axis in PGFPLOTS

I am trying to draw a mesh in the domain { (x,y,z) in [0,4] }. I was able to constrain the domain in the x-axis and y-axis, but the mesh is still drawn in the negative z-axis (Image 1), which is outside the desired domain of [0:4]. I also tried the above with different plot types without success.

I was able to "hack" a solution by adding restrict z to domain= 0:4 (Image 2) and completing the lattice with coordinates (Image 3).

While I was able to hack together a solution, I am looking for a more elegant solution using only one \addplot3. Any suggestions are appreciated.

Undesired result: Source code for undesired result:

\begin{tikzpicture}
\begin{axis}[axis lines = center,
y dir = reverse,
xmin=0, xmax=5,
ymin=0, ymax=5,
zmin=0, zmax=5]
\addplot3[mesh,
draw = black,
samples = 5,
domain = 0:4]
{4-x-y};
\end{axis}
\end{tikzpicture}


Result when adding restrict z to domain= 0:4: Desired result (adding coordinates to complete the above lattice):

Added code: \addplot3 [mark = none, draw = black]coordinates {(3,0,1) (4,0,0) (0,4,0) (0,3,1)}; ## 1 Answer

Here is a possible approach with only one addplot3: MWE:

\documentclass[]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\begin{document}
\pgfplotsset{compat=1.17}
\begin{tikzpicture}
\begin{axis}[axis lines = center,
y dir = reverse,
xmin=0, xmax=5,
ymin=0, ymax=5,
zmin=0, zmax=5,
]
\addplot3[
patch,
patch refines=4,
mesh,
draw = black,
domain = 0:4,
y domain = 0:4,
]
table[z expr=4-x-y]
{
x  y
0  0
0  4
4  0
};
\end{axis}
\end{tikzpicture}
\end{document}


You could also plot it with the coordinates (at least for this simple example):

\documentclass[]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\begin{document}
\pgfplotsset{compat=1.17}
\begin{tikzpicture}
\begin{axis}[axis lines = center,
y dir = reverse,
xmin=0, xmax=5,
ymin=0, ymax=5,
zmin=0, zmax=5,
]
\addplot3[
patch,
patch refines=4,
mesh,
draw = black,
domain = 0:4,
y domain = 0:4,
]
coordinates {
(0,0,4) (0,4,0) (4,0,0)
};
\end{axis}
\end{tikzpicture}
\end{document}