1

I want to use pgfplots to plot the the external data. The data is from a function and I have calculated it using mathematica. Both the function values and the grads have been obtained. I want to use surf plot to show the value and the quiver plot to show the grads.

Here is a mwe, the function value is in the value.dat while the function grad is in the grad.dat

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{value.dat}
    x   y   z
    0.  0.  0.
    0.  0.5 0.479426
    0.  1.  0.841471
    0.5 0.  0.
    0.5 0.5 0.790439
    0.5 1.  1.38735
    1.  0.  0.
    1.  0.5 1.30321
    1.  1.  2.28736
\end{filecontents}
\begin{filecontents}{grad.dat}
    u   v
    0.  1.
    0.479426    0.877583
    0.841471    0.540302
    0.  1.64872
    0.790439    1.44689
    1.38735 0.890808
    0.  2.71828
    1.30321 2.38552
    2.28736 1.46869
\end{filecontents}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            height=12cm,
            width=15cm,
            grid=major,
            grid style={dotted},
            mesh/ordering=x varies,
            mesh/cols=3
            ]
            \addplot3[surf, shader=flat] table {value.dat};
            \addplot3[
                quiver=
                {u=\thisrow{u},
                 v=\thisrow{v},
                 w=3,
                 scale arrows=0.5},
                 -stealth,
                ] table {grad.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}

The code above gives the error saying that

the requested column number '2' in table 'grad.dat' does not exist!

How can I get this code work?

3

I think what you mean to do is plot the vector field along the surface.

If this is right, then we need to ask PGFplots to do so.

We merge both tables for convenience, where :

  • the three first columns indicate from where the arrows are meant to be drawn,
  • while the two last indicate what arrows should be drawn

The output

enter image description here

Doesn't look much like a gradient vector field to me... Aren't the arrows supposed to be tangent to the surface ?

If so, we need to set w=\thisrow{u}^2+\thisrow{v}^2, and we get :

enter image description here

The code

\documentclass[tikz, border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=newest}
% we merge both tables :
\begin{filecontents}{valuesAndGrad.dat}
    x   y   z        u           v       
    0.  0.  0.       0.          1.      
    0.  0.5 0.479426 0.479426    0.877583
    0.  1.  0.841471 0.841471    0.540302
    0.5 0.  0.       0.          1.64872 
    0.5 0.5 0.790439 0.790439    1.44689 
    0.5 1.  1.38735  1.38735     0.890808
    1.  0.  0.       0.          2.71828 
    1.  0.5 1.30321  1.30321     2.38552 
    1.  1.  2.28736  2.28736     1.46869 
\end{filecontents}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [
      %unit vector ratio = 1 1 1, % consider this option
      height=12cm,
      width=15cm,
      grid=major,
      grid style={dotted},
      mesh/ordering=x varies,
      mesh/cols=3
    ]
    \addplot3
    [
      quiver=
      {
        u=\thisrow{u},
        v=\thisrow{v},
        w=1,% 3 looked strange...
        scale arrows=.3,
      },
      -stealth,
      blue,
    ] 
    table {valuesAndGrad.dat};
    \addplot3[surf, shader=flat, opacity=.3] table {valuesAndGrad.dat};
  \end{axis}
\end{tikzpicture}
\end{document}

Cheers,

5
  • Thanks for your answer. I read your answer and got what I want. – Ice0cean Feb 21 '17 at 10:56
  • @Ice0cean, if marsupilam's answer helped you, how about upvoting it and if it solved your problem, also accept it? – Stefan Pinnow Feb 21 '17 at 11:28
  • Thanks for your reminding @Stefan Pinnow. I have upvoted it and accepted for the answer. I also apology for my careless :( – Ice0cean Feb 23 '17 at 6:37
  • Thanks to you both ! @IceOcean : as of this message, the answer has not been accepted. If you do want to accept it, click on the gray tick on the left below the voting arrows (more explanations) – marsupilam Feb 23 '17 at 6:53
  • @marsupilam Thanks. I'm not very familiar with the button in stackexchange. I have read the links your provide and know that not the triangle but the check mark is the button to accept answer. – Ice0cean Feb 23 '17 at 8:03

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.