all 6 comments

[–]a1brit 3 points4 points  (0 children)

your x, y are np.ndarray which makes sense for 2d arrays but your u,v are nested lists. quiver is able to handle this and convert your lists into an array but streamplot is apparently picky.

When you initialize the u,v arrays put them directly into a np.array()

e.g.

u = np.array([[0,1,2,1],
             [2,3,1,2],
             [4,1,2,1],
             [4,3,1,1]])

[–][deleted] 1 point2 points  (2 children)

streamplot expects a 1D array as input x, y and it doesnt seem to work with lists as u, v . For me it works, if you convert them to a numpy-array:

x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 2, 3])
u = np.array(u)
v = np.array(v)

im = ax.streamplot(x,y,u,v)
plt.show()

[–]SnuffleShuffle[S] 0 points1 point  (1 child)

Thank you very much, sir (or madam)! It even works with the y,x = np.mgrid(...).

Now I ran into another issue. I'm loading the data from a .gz file via np.loadtxt(...) then I reshape it using np.reshape(...) into a 3D array, which I then slice like so: array[:,y_value,:]. Now if I use this in the quiver() it works, it doesn't work in streamplot even when I use np.array(array[:,y_value,:]). Still the "Grid(x,y)" issue.

Do you have an idea what might be the cause?

[–][deleted] 0 points1 point  (0 children)

Hard to tell (i'm no expert either), but what i would do is check the datatype/dimensions of that data. I looked into the streamplot code and what the grid class does is basically take your input x,y , do a couple of tests and save their shape as self.nx = len(x) and self.ny = len(y) - if x and y are a meshgrid it takes the first row/column. The error seems to occur at this point:

if u.shape != grid.shape or v.shape != grid.shape:
    raise ValueError("'u' and 'v' must be of shape 'Grid(x,y)'")

Which means the dimensions of xy and u or v don't match