2

Assumed we draw a simple 3D plot with pgfplots.


Minimum Working Example (MWE):

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
    x;y;z
    0;0;4
    0;1;4
    0;2;4
    1;0;3
    1;1;3
    1;2;3
    2;0;2
    2;1;2
    2;2;2
    3;0;1
    3;1;1
    3;2;1
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[table/col sep = semicolon]
            \addplot3[mesh, scatter, mesh/cols=3] table[x=x, y=y, z=z] {testdata.csv};
        \end{axis}
    \end{tikzpicture}
\end{document}

Screenshot of the result:

Screenshot of the result


Question:

  • Would it be possible to draw the mesh only in y direction, but not in x direction? You can see the desired result in the next screenshot
  • The mesh colors depending on the z-value can be preserved

Screenshot of the desired state:

Screenshot of the desired state

2

You could just loop over ordinary plots.

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
    x;y;z
    0;0;4
    0;1;4
    0;2;4
    1;0;3
    1;1;3
    1;2;3
    2;0;2
    2;1;2
    2;2;2
    3;0;1
    3;1;1
    3;2;1
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[table/col sep = semicolon]
            \pgfplotsinvokeforeach{0,...,3}
            {\addplot3[scatter,%mesh, scatter, mesh/cols=3,
             %x filter/.expression={x == #1 ? x : nan}
             restrict x to domain=#1:#1] table[x=x, y=y, z=z] {testdata.csv};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

If you want to keep the colors of the lines, you could do

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
    x;y;z
    0;0;4
    0;1;4
    0;2;4
    1;0;3
    1;1;3
    1;2;3
    2;0;2
    2;1;2
    2;2;2
    3;0;1
    3;1;1
    3;2;1
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[table/col sep = semicolon]
            \pgfplotsinvokeforeach{0,...,3}
            {\addplot3[scatter,mesh, scatter, mesh/cols=1,
             %x filter/.expression={x == #1 ? x : nan}
             restrict x to domain=#1:#1
             ] table[x=x, y=y, z=z] {testdata.csv};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much! What does \pgfplotsinvokeforeach do exactly? I understand it repeats drawing the plot, but why is necessary to limit it to a specific range/domain like {0,...,3}? – Dave May 18 at 7:52
  • @Dave It is like a \foreach loop except that it avoids some expansion problems. – user121799 May 18 at 7:55
  • Hmmm, okay, I never used that before - seems to be a bit complicated. :-) I just repeat drawing the plot with this, no? Thank you! – Dave May 18 at 8:15

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.