16

This question originates in Scalar field in pgfplots .

How can I draw a quiver plot in which the arrows have the same length, but their line width varies according to some scalar property of the vectors?

1 Answers1

14

Pgfplots allows to employ point meta as additional input coordinate. Typically, point meta is used as color data, but it can also be used for different purposes.

In this case, you can use it as line thickness parameter for a quiver plot:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\def\U{1}
\def\V{2*x}
\def\LEN{(sqrt((\U)^2 + (\V)^2)}
\begin{axis}[axis equal]
    \addplot[blue,
        point meta={\LEN},
        quiver={
            u={(\U)/\LEN},v={(\V)/\LEN},
            scale arrows=2,
            every arrow/.append style={line width=\pgfplotspointmetatransformed/1000 * 2pt},
        },
        -stealth,samples=15,
    ] {x^2};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

The example defines some macros \U, \V, and \LEN where \LEN is the length of these vectors. Then, a quiver plot is drawn where each vector is normalized to unit length - and the length parameter is assigned as point meta. This is a simple and standard way of communicating extra data to plots (you could also input extra values in a table).

Finally, \pgfplotspointmetatransformed is a scalar value in the range [0,1000] such that 0 corresponds to the minimum point meta value and 1000 corresponds to the maximum point meta value.

The expression for the line width scales this linearly such that the minimum line thickness is 0 and the maximum is 2pt.

Note that axis equal is necessary if we want to see that the vectors have the same sizes.