Quiver Plots in Matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Define grid
X, Y = np.meshgrid(np.arange(-2, 3, 1), np.arange(-2, 3, 1))

# Define vector components with the correct shape (5x5 grid)
U = np.ones_like(X)  # U components all set to 1
V = np.array([[1, -1, 1, -1, 1], [-1, 1, -1, 1, -1], [1, -1, 1, -1, 1], [-1, 1, -1, 1, -1], [1, -1, 1, -1, 1]])

fig, ax = plt.subplots()
ax.quiver(X, Y, U, V)

ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
plt.show()
import numpy as np
import matplotlib.pyplot as plt

# Define grid
X, Y = np.meshgrid(np.arange(-2, 3, 1), np.arange(-2, 3, 1))

# Define vector components with the correct shape (5x5 grid)
U = np.ones_like(X)  # U components all set to 1
V = np.array([[1, -1, 1, -1, 1], [-1, 1, -1, 1, -1], [1, -1, 1, -1, 1], [-1, 1, -1, 1, -1], [1, -1, 1, -1, 1]])

fig, ax = plt.subplots()

# Draw arrows with customizations
ax.quiver(
    X, Y, U, V, 
    color='blue', angles='xy', scale_units='xy', scale=2, width=0.005
)

ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
plt.show()
import numpy as np
import matplotlib.pyplot as plt

# Define grid
X, Y = np.meshgrid(np.arange(-2, 3, 1), np.arange(-2, 3, 1))

# Define vector components
U = np.cos(X)
V = np.sin(Y)

# Calculate magnitude
magnitude = np.sqrt(U**2 + V**2)

fig, ax = plt.subplots()

# Draw arrows with magnitude represented by color
quiver = ax.quiver(
    X, Y, U, V, magnitude, 
    angles='xy', scale_units='xy', scale=1, width=0.005, 
    cmap='viridis'
)

# Add color bar to indicate magnitude
fig.colorbar(quiver, ax=ax)

ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Generate a grid of points
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)

# Define vector components
U = -Y
V = X

# Create a quiver plot
plt.figure()
Q = plt.quiver(X, Y, U, V, scale=40)
plt.quiverkey(Q, X=0.8, Y=1.05, U=0.5, label='0.5 units', labelpos='E')

plt.title('Quiver Plot with Quiver Key')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
import matplotlib.pyplot as plt
import numpy as np

# Generate a grid of points
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)

# Define vector components for circular flow
U = -Y / (X**2 + Y**2)
V = X / (X**2 + Y**2)

# Create a quiver plot
plt.figure()
plt.quiver(X, Y, U, V)
plt.title('Circular Flow Pattern')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
CTRL + ENTER to send