Mac - 1 year ago 119

Python Question

I am trying to write a very simple code (should be):

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.

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?

`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)

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You would benefit from a quick sanity check.

```
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)
```

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.

In such a case, you just need to flatten `x`

and `y`

to make them 1-dimensional. You can't mix 1D and 2D arrays in quiver.

```
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)
```

**EDIT:** 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?

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**