1

I have a vector field:

enter image description here

...but when I want to plot the associated streamplot, I get an error:

ValueError: The rows of 'x' must be equal

Here is my code:

import matplotlib.pyplot as plt
import numpy as np


NY = 20; ymin = -2.; ymax = 2.
dy = (ymax -ymin )/(NY-1.)
NX = NY
xmin = -2.; xmax = 2.
dx = (xmax -xmin)/(NX-1.)


y = np.array([ ymin + float(i)*dy for i in range(NY)])
x = np.array([ xmin + float(i)*dx for i in range(NX)])

x, y = np.meshgrid( x, y, indexing = 'ij', sparse = False)

Fx  = np.cos(x + 2*y)
Fy  = np.sin(x - 2*y)

F = np.array([Fx, Fy])

# plotting the vectors
plt.quiver(x,y,Fx,Fy)
# Plotting stream plot 
plt.streamplot(x, y, Fx, Fy)

plt.show()

Anyone knows why? I have checked the sizes, but they seem equal to me...

Edit:

From the comment section by Trenton McKinney:

" Note xx and yy in this answer. Each row is the same, but the values in each row are increasing. The values in each of your rows are the same, but each row is increasing. "

enter image description here

... I tried:

plt.streamplot(np.transpose(x), np.transpose(y), Fx, Fy)

but as you can see the streamplot is not correct.

0

1 Answer 1

1

Thanks to the comment from TrentonMcKinney I realized what the issue was:

In my case:

The values in each of my rows are the same, but each row is increasing.

But what I need for streamplot to work is:

Each row is the same, but the values in each row are increasing.

So I changed indexing = 'ij' to = 'xy':

x, y = np.meshgrid( x, y, indexing = 'xy', sparse = False)

Complete code:

import matplotlib.pyplot as plt
import numpy as np


NY = 20; ymin = -2.; ymax = 2.
dy = (ymax -ymin )/(NY-1.)
NX = NY
xmin = -2.; xmax = 2.
dx = (xmax -xmin)/(NX-1.)


y = np.array([ ymin + float(i)*dy for i in range(NY)])
x = np.array([ xmin + float(i)*dx for i in range(NX)])

x, y = np.meshgrid( x, y, indexing = 'xy', sparse = False)

Fx  = np.cos(x + 2*y)
Fy  = np.sin(x - 2*y)

F = np.array([Fx, Fy])

# plotting the vectors
plt.quiver(x,y,Fx,Fy)
# Plotting stream plot 
plt.streamplot(x, y, Fx, Fy)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.