Python How to plot vector fields in Matplotlib

Click For Summary
The discussion revolves around plotting a vector field defined by the equation F = ye^x i + (x^2 + e^x) j + z^2e^z k using Python's NumPy and Matplotlib libraries. The user is struggling with normalizing the arrow lengths in the plot to prevent them from being excessively long. They provide their initial code for defining the vector field components, creating a grid, and attempting to standardize the arrow lengths using the formula for length. The user seeks clarification on how to properly implement the normalization of the arrows, referencing an example of a simpler vector field plot for guidance. They also mention a related question posted on Stack Overflow and provide a link to Matplotlib's documentation, noting that examples there use parameters like normalize=True and length=0.1 in the quiver function. The focus is on achieving a visually coherent representation of the vector field.
JD_PM
Messages
1,125
Reaction score
156
Hi, I want to plot the vector field ##\vec F = ye^x \hat i + (x^2 + e^x) \hat j + z^2e^z \hat k##

The code I have tried:

Python:
# The components of the vector field

F_x = y*e**x

F_y = x**2 + e**x

F_z = z**2*e**z# The grid

xf = np.linspace(-0.15, 2.25, 8)

yf = np.linspace(-0.15, 2.25, 8)

zf = np.linspace(-0.75, 2.50, 8)

X_grid, Y_grid, Z_grid = np.meshgrid(xf, yf, zf)# The arrows; how to deal with them?

dx = 1

#dy = ...

#dz = ...# Standardize the arrows; In this way all arrows have the same length.

length = np.sqrt(dx**2 + dy**2 + dz**2)

dx_N = dx/length

dy_N = dy/length

dz_N = dz/length#how to involve numpy in the process??# Drawing the figure

fig, ax = plt.subplots(1, 1)

ax.quiver(X_grid, Y_grid, Z_grid, dx_N, dy_N, dz_N, dy, dz, cmap=plt.get_cmap('gnuplot2'))

plt.show()

I am stuck in how to make sure that the arrows are not too long (I know I have to use length, but how?).

I attach next an example I am following of a plot of another vector field (##V = \hat i + xy \hat j##).

Python:
# The function to be applied
def rightmember(x, y):
    return x*y

# The grid
x = np.linspace(-3, 3, 25)
y = np.linspace(-3, 3, 25)
X_grid, Y_grid = np.meshgrid(x, y)

# The arrows
dx = 1
dy = rightmember(X_grid, Y_grid)

# Standardize the arrows; In this way all arrows have the same length.
length = np.sqrt(dx**2 + dy**2)
dx_N = dx/length
dy_N = dy/length

# Drawing the figure
fig, ax = plt.subplots(1, 1)
ax.quiver(X_grid, Y_grid, dx_N, dy_N, dy, cmap=plt.get_cmap('gnuplot2'))

plt.show()

I have posted the same question on SO: https://stackoverflow.com/questions/55759028/how-to-plot-a-vector-field-using-numpy

Thank you for your help.
 
Technology news on Phys.org
I am not too knowledgeable about vector plots in Python, however, a quick google search yielded: https://matplotlib.org/gallery/mplot3d/quiver3d.html
and they seem to have set:
- normalize = True
- length = 0.1

in the ax.quiver() in line 21...

Hope that is of some use.
 
  • Like
Likes JD_PM
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 21 ·
Replies
21
Views
5K
  • · Replies 14 ·
Replies
14
Views
6K
Replies
4
Views
4K