Making a quiver plot from .dat files
<p>Hi I am trying to make a quiver (vector field) plot from data that is stored in .dat files.  I have 4 .dat files which are 1D arrays, one for the x axis, y axis, f(x,y) along x and f(x,y) along y.</p>

<p>Note, I am able to construct a quiver plot without importing data from .dat files, I just followed this basic example <a href="http://www.scipy-lectures.org/intro/matplotlib/auto_examples/plot_quiver_ex.html" rel="nofollow">here</a>.  </p>

<p>However, I am unable to apply this basic example to my example in which I need to import the data from .dat files.  My code is below, I am not getting any error messages but I am getting a blank quiver plot.  Any help/suggestions would be greatly appreciated, thanks!</p>

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

n=12

data0 = np.genfromtxt('xaxis.dat')
data1 = np.genfromtxt('yaxis.dat')
data2 = np.genfromtxt('fx.dat')
data3 = np.genfromtxt('fy.dat')

x  = data0[0]
y  = data1[0]
fx = data2[0]
fy = data3[0]

plt.axes([0.025, 0.025, 0.95, 0.95])
plt.quiver(x,y,fx,fy, alpha=.5)
plt.quiver(x,y,fx,fy,edgecolor='k',facecolor='none', linewidth=.5)

plt.xlim(-1,n)
plt.xticks(())
plt.ylim(-1,n)
plt.yticks(())

plt.show()
</code></pre>

<p>In the <a href="http://www.scipy-lectures.org/intro/matplotlib/auto_examples/plot_quiver_ex.html" rel="nofollow">example for the quiver plot</a> you provided all <code>X</code>, <code>Y</code>, <code>U</code> and <code>V</code> are 2D arrays, with shape <code>(n,n)</code>.</p>

<p>In your example you are importing an array of values for <code>x</code>, <code>y</code>, <code>fx</code> and <code>fy</code>, and then selecting only the first line with <code>[0]</code>.</p>

<p>When using the code:</p>

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

n=3 # number of points, changed it

data0 = np.genfromtxt('xaxis.dat')
data1 = np.genfromtxt('yaxis.dat')
data2 = np.genfromtxt('fx.dat')
data3 = np.genfromtxt('fy.dat')

x  = data0[0]
y  = data1[0]
fx = data2[0]
fy = data3[0]

plt.axes([0.025, 0.025, 0.95, 0.95]) # position of bottom left point of graph inside window and its size
plt.quiver(x,y,fx,fy, alpha=.5) # draw inside of arrows, half transparent
plt.quiver(x,y,fx,fy,edgecolor='k',facecolor='none', linewidth=.5) # draw contours of arrows

plt.xlim(-1,n) # left and right most values in the x axis
plt.xticks(()) # remove the numbers from the x axis
plt.ylim(-1,n) # ...
plt.yticks(()) # ...

plt.show()
</code></pre>

<p>I get:
<a href="https://i.stack.imgur.com/W9HoF.png" rel="nofollow"><img src="https://i.stack.imgur.com/W9HoF.png" alt="only one point"></a>
With <code>0 1 2 0 1 2 0 1 2</code> in xaxis.dat and fx.dat, <code>0 0 0 1 1 1 2 2 2</code> in yaxis.dat and <code>1 1 1 2 2 2 3 3 3</code> in fy.dat.
If I just remove the <code>[0]</code> from the arrays assignment, I get:
<a href="https://i.stack.imgur.com/Vcbvv.png" rel="nofollow"><img src="https://i.stack.imgur.com/Vcbvv.png" alt="all points"></a>
with all points shown.</p>

<p>One change I would make is to use <code>plt.xlim(min(x)-1,max(x)+1)</code> and <code>plt.ylim(min(y)-1,max(y)+1)</code>, to ensure you get to view the right area of the graph. For instance, if I make all four arrays equal to <code>np.random.rand(10)</code> (a 1D array with 10 random elements between 0 and 1), I get:
<a href="https://i.stack.imgur.com/NQkAv.png" rel="nofollow"><img src="https://i.stack.imgur.com/NQkAv.png" alt="random points"></a></p>

<h2>Notes on array format</h2>

<p>The <code>plt.quiver</code> will also accept the arrays in the format:</p>

<pre><code>x  = [0, 1, 2] # 1D array (list, actually...)
y  = [0, 1, 2]
fx = [[0, 1, 2],
      [0, 1, 2],
      [0, 1, 2]] # 2D array
fy = [[0, 0, 0],
      [1, 1, 1],
      [2, 2, 2]]
</code></pre>

<p><a href="https://i.stack.imgur.com/q95SE.png" rel="nofollow"><img src="https://i.stack.imgur.com/q95SE.png" alt="enter image description here"></a>
But not if all arrays are 1D:</p>

<pre><code>fx = np.array(fx).flatten()
fy = np.array(fy).flatten()
</code></pre>

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

<h2>Previous answer (wrong)</h2>

<p>[first two paragraphs]...</p>

<p>This means you probably noticed <code>genfromtxt</code> returns a 2D array (as it is able to import several columns from a single file, so the returned array will mimic the 2D structure of your file if nothing else is told), making <code>data0[0]</code> the first line on your document xaxis.dat.</p>

<p><strong>EDIT:</strong> the sentence below is erroneous, <a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.quiver" rel="nofollow">plt.quiver</a> can receive 1D arrays, just in the right shape.</p>

<p>However the <code>quiver</code> expects 2D arrays, from where it will retrieve the values for each point: for point <code>i,j</code> the position will be <code>(X[i,j], Y[i,j])</code> and the arrow will be <code>(U[i,j], V[i,j])</code>.</p>

<p>If you have the repeated values for x and y in the file like this:</p>

<ul>
<li><p>xaxis.dat:</p>

<p>0, 1, 2, 0, 1, 2, 0, 1, 2</p></li>
<li><p>yaxix.dat:</p>

<p>0, 0, 0, 1, 1, 1, 2, 2, 2</p></li>
</ul>

<p>You can just <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html" rel="nofollow">reshape</a> all four of your arrays to (# points in x, # points in y) and it should work out.</p>

<p>If you don't you will have to use something similar to <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html" rel="nofollow"><code>np.mgrid</code></a> (or <a href="http://louistiao.me/posts/numpy-mgrid-vs-meshgrid/" rel="nofollow"><code>np.meshgrid</code></a>) to make a valid combination of <code>X</code> and <code>Y</code> arrays, and format <code>fx</code> and <code>fy</code> accordingly.</p>

python matplotlib plot