Arrow colour quiver plot - Python
<p>I am plotting an arrow graph and my code uses an external file as follows:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from pylab import rcParams

data=np.loadtxt(r'data.dat')

x = data[:,0] 
y = data[:,1] 
u = data[:,2] 
v = data[:,3] 


plt.quiver(x, y, u, v, angles='xy', scale_units='xy', scale=1, pivot='mid',color='g')
</code></pre>

<p>The data file basically looks like :</p>

<pre><code>0 0 0 1
0 1 1 0
1 0 1 0
1 1 0 1
</code></pre>

<p>that generates a plot that looks like </p>

<p><a href="https://i.stack.imgur.com/jkFgL.png" rel="nofollow"><img src="https://i.stack.imgur.com/jkFgL.png" alt="enter image description here"></a></p>

<p>Is there a way to plot this with different colours for the different arrow directions?</p>

<p>Ps.: I have got a lot more arrows in my data file in a not very logical sentence like the one I am using as example.</p>

<p>This probably do the trick:</p>

<pre><code>plt.quiver(x, y, u, v, np.arctan2(v, u), angles='xy', scale_units='xy', scale=1, pivot='mid',color='g')
</code></pre>

<p>Note that the fifth's argument of <code>plt.quiver</code> is a color.
<a href="https://i.stack.imgur.com/Nc5mK.png" rel="nofollow"><img src="https://i.stack.imgur.com/Nc5mK.png" alt="colored arrows"></a></p>

<hr>

<p>UPD. If you want to control the colors, you have to use <a href="http://matplotlib.org/users/colormaps.html" rel="nofollow">colormaps</a>. Here are a couple of examples:</p>

<p>Use colormap with <code>colors</code> parameter:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize

%matplotlib inline

ph = np.linspace(0, 2*np.pi, 13)
x = np.cos(ph)
y = np.sin(ph)
u = np.cos(ph)
v = np.sin(ph)
colors = arctan2(u, v)

norm = Normalize()
norm.autoscale(colors)
# we need to normalize our colors array to match it colormap domain
# which is [0, 1]

colormap = cm.inferno
# pick your colormap here, refer to 
# http://matplotlib.org/examples/color/colormaps_reference.html
# and
# http://matplotlib.org/users/colormaps.html
# for details
plt.figure(figsize=(6, 6))
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.quiver(x, y, u, v, color=colormap(norm(colors)),  angles='xy', 
           scale_units='xy', scale=1, pivot='mid')
</code></pre>

<p><a href="https://i.stack.imgur.com/EWeAj.png" rel="nofollow"><img src="https://i.stack.imgur.com/EWeAj.png" alt="different colormap"></a></p>

<p>You can also stick with fifth argument like in my first example (which works in a bit different way comparing with <code>colors</code>) and change default colormap to control the colors.</p>

<pre><code>plt.rcParams['image.cmap'] = 'Paired'

plt.figure(figsize=(6, 6))
plt.xlim(-2, 2)
plt.ylim(-2, 2)

plt.quiver(x, y, u, v, np.arctan2(v, u), angles='xy', scale_units='xy', scale=1, pivot='mid')
</code></pre>

<p><a href="https://i.stack.imgur.com/9evy3.png" rel="nofollow"><img src="https://i.stack.imgur.com/9evy3.png" alt="Paired colormap"></a></p>

<p>You can also create your own colormaps, see e.g. <a href="http://stackoverflow.com/questions/16834861/create-own-colormap-using-matplotlib-and-plot-color-scale">here</a>.</p>

python arrays matplotlib plot arrow