2

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: enter image description here 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: enter image description here 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)}; enter image description here

0

Here is a possible approach with only one addplot3:

enter image description here

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}

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.