How to use matplotlib quiver using an external file
<p>I am trying to write a very simple code (should be):</p>

<p>I want to plot arrows from an external data file which gives me the vector dimensions in x and y, given in two columns sx and sy.</p>

<p>For example, I have two columns of 36 numbers each but they are the dimension of a vector having 6x6 grid. however when I do the following it gives me an error, I suppose I need an extra step to convert this data from two columns to a grid?!
But I have no idea what this step could be. Any insights?</p>

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

data=np.loadtxt(r'text.dat')
x,y = np.meshgrid(np.arange(0, 6, 1), np.arange(0, 6, 1))
u = data[:,1]
v = data[:,2]

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

<p>You would benefit from a quick sanity check.</p>

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

data=np.loadtxt(r'text.dat')
x,y = np.meshgrid(np.arange(0, 6, 1), np.arange(0, 6, 1))

print x  # 6x6 array, 2D!
print y  # 6x6 array, 2D!

u = data[:,1] # 36 array, 1D!
v = data[:,2] # 36 array, 1D!

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

<p>Presumably your text.dat file holds records for points [(0,0), (1,0), (2,0), ... (0,1), (1,1), (2,1)... (4,5), (5,5)] in that order.</p>

<p>In such a case, you just need to flatten <code>x</code> and <code>y</code> to make them 1-dimensional. You can't mix 1D and 2D arrays in quiver.</p>

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

data=np.loadtxt(r'text.dat')
x,y = np.meshgrid(np.arange(0, 6, 1), np.arange(0, 6, 1))
x = x.flatten()
y = y.flatten()

print x  # 36 array, 1D!
print y  # 36 array, 1D!

u = data[:,1] # 36 array, 1D!
v = data[:,2] # 36 array, 1D!

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

<p><strong>EDIT:</strong> If you continue having problems, run your commands in an interactive terminal or command prompt. Check each variable and its dimensions (array.shape) to make sure the variables are what you think they are. Are each of the dimensions actually 36? </p>

python matplotlib plot vector-graphics