4

In the following mwe, my goal is to have the arrows point in the arbitrary direction given by the third column of the data.

the mwe image

After reading some answers (for example from Dr. Feursänger there or this other question), I started to define my marker, with the hope to rotate it. I couldn't go any further. Is there a simple solution ? The image is generated with:

\documentclass{standalone}
\usepackage{pgfplots}


\begin{document}

\pgfplotstableread{
  5.30E-001 5.30E-001   -7.85E-001
  5.96E-001 5.89E-001   -7.85E-001
  6.69E-001 6.39E-001   -7.85E-001
  7.47E-001 6.81E-001   -7.85E-001
  8.29E-001 7.13E-001   -7.85E-001
  9.14E-001 7.36E-001   -7.84E-001
  1.00E+000 7.48E-001   -7.81E-001
  1.09E+000 7.49E-001   -7.72E-001
  1.18E+000 7.41E-001   -7.55E-001
  1.26E+000 7.22E-001   -7.28E-001
  1.35E+000 6.93E-001   -6.91E-001
  1.43E+000 6.54E-001   -6.43E-001
  1.50E+000 6.07E-001   -5.88E-001
  1.57E+000 5.51E-001   -5.25E-001
  1.63E+000 4.87E-001   -4.57E-001
  1.68E+000 4.17E-001   -3.85E-001
  1.73E+000 3.40E-001   -3.11E-001
  1.76E+000 2.60E-001   -2.34E-001
  1.79E+000 1.75E-001   -1.57E-001
  1.81E+000 8.82E-002   -7.85E-002
}\datatable

\begin{tikzpicture}[trim axis left, trim axis right]
  \pgfdeclareplotmark{yawarrow}{%
    \draw[->] (0,0)--(0,100);
  }
  \begin{axis}
    [
      xlabel={$x$},
      ylabel={$y$},
    ]
    \addplot[%
      mark=yawarrow,
 %     mark options={rotate=\thisrowno{2}}, % This won't work
    black] table[x index={0},y index={1}] {\datatable};

  \end{axis}
\end{tikzpicture}

\end{document}

Edit

Based on the answer to this question, I've tried to update my code as follows. This fails with Undefined control sequence \yaw. Am I missing something on the use of the option and on of the \yaw ?

\pgfplotstablegetrowsof{\datatable} 
\pgfmathsetmacro{\rows}{\pgfplotsretval-2}

\begin{tikzpicture}[trim axis left, trim axis right]
  \pgfdeclareplotmark{yawarrow}{%
    \draw[->] (0,0)--(0,100);
  }
  \begin{axis}
    [
      xlabel={$x$},
      ylabel={$y$},
    ]
      \foreach \k in {0,...,\rows}
      {
    \pgfplotstablegetelem{\k}{[index]0}\of\datatable 
      \let\x\pgfplotsretval
      \pgfplotstablegetelem{\k}{[index]1}\of\datatable 
      \let\y\pgfplotsretval
      \pgfplotstablegetelem{\k}{[index]2}\of\datatable 
      \let\yaw\pgfplotsretval
      \pgfmathsetmacro{\K}{\k+1}
      \pgfplotstablegetelem{\K}{[index]0}\of\datatable 
      \let\X\pgfplotsretval
      \pgfplotstablegetelem{\K}{[index]1}\of\datatable 
      \let\Y\pgfplotsretval
      \addplot [
        mark=yawarrow,
        mark options={rotate=\yaw}, %Won't work either
      ] coordinates {(\x,\y) (\X,\Y)};
    }
    \end{axis}
  \end{tikzpicture}
5

I would do this using a quiver plot instead of rotating markers:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}


\begin{document}

\begin{filecontents}{data.dat}
  5.30E-001 5.30E-001   -7.85E-001
  5.96E-001 5.89E-001   -7.85E-001
  6.69E-001 6.39E-001   -7.85E-001
  7.47E-001 6.81E-001   -7.85E-001
  8.29E-001 7.13E-001   -7.85E-001
  9.14E-001 7.36E-001   -7.84E-001
  1.00E+000 7.48E-001   -7.81E-001
  1.09E+000 7.49E-001   -7.72E-001
  1.18E+000 7.41E-001   -7.55E-001
  1.26E+000 7.22E-001   -7.28E-001
  1.35E+000 6.93E-001   -6.91E-001
  1.43E+000 6.54E-001   -6.43E-001
  1.50E+000 6.07E-001   -5.88E-001
  1.57E+000 5.51E-001   -5.25E-001
  1.63E+000 4.87E-001   -4.57E-001
  1.68E+000 4.17E-001   -3.85E-001
  1.73E+000 3.40E-001   -3.11E-001
  1.76E+000 2.60E-001   -2.34E-001
  1.79E+000 1.75E-001   -1.57E-001
  1.81E+000 8.82E-002   -7.85E-002
\end{filecontents}

\begin{tikzpicture}
  \begin{axis}
    [
      xlabel={$x$},
      ylabel={$y$},
    ]
    \addplot[quiver={
        u={sin(\thisrowno{2} r + 90)},
        v={cos(\thisrowno{2} r + 90)},
        scale arrows=0.1
    }, -latex] table[x index={0},y index={1}] {data.dat};
    \addplot [thick] table {data.dat};
  \end{axis}
\end{tikzpicture}

\end{document}
  • As far I can guess, the quiver marks can be changed to other marks and this solution is applicable to other problems. Should I give another title to my question to reflect the approach of your answer ? – Vser May 3 '15 at 17:03
  • @Vser: The quivers can be changed to use other arrow heads, but you can't easily use general plot marks (rotated squares without arrow lines, say). But I think this would still apply to most use cases. Yes, it would probably be good to rename this question so people actually needing a way to rotate plot marks (as opposed to needing a way to draw rotated arrows) won't be disappointed. – Jake May 3 '15 at 17:06
  • 1
    I've tried to reflect your (accurate) comment in the title. I hope it's understandable. – Vser May 3 '15 at 17:13

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.