0

I am trying to build an animation of a 3D quiver plot. I've been able to do this using quiver.set_UVC() in 2D quiver plots. It seems however that this function is not implemented for 3D quiver plots. This is for example a code suggested by ChatGPT

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

# Create data
x = np.linspace(-2, 2, 10)
y = np.linspace(-2, 2, 10)
z = np.linspace(-2, 2, 10)
X, Y, Z = np.meshgrid(x, y, z)
U = X  # x-component of the vectors
V = Y  # y-component of the vectors
W = Z  # z-component of the vectors

# Create the figure and 3D axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Initialize the quiver plot
quiver = ax.quiver(X, Y, Z, U, V, W)

# Update function for each frame
def update(frame):
    # Change arrow properties for each frame
    U_updated = U * frame
    V_updated = V * frame
    W_updated = W * frame
    quiver.set_UVC(U_updated, V_updated, W_updated)
    return quiver

# Create the animation
animation = FuncAnimation(fig, update, frames=10, interval=200, blit=False)

# Set axes labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Display the animation
plt.show()

It gives the error:

AttributeError: 'Line3DCollection' object has no attribute 'set_UVC'

My attempt at a work around was

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

# Create data
x = np.linspace(-2, 2, 10)
y = np.linspace(-2, 2, 10)
z = np.linspace(-2, 2, 10)
X, Y, Z = np.meshgrid(x, y, z)
U = X  # x-component of the vectors
V = Y  # y-component of the vectors
W = Z  # z-component of the vectors

# Create the figure and 3D axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Initialize the quiver plot
quiver = ax.quiver(X, Y, Z, U, V, W)

# Update function for each frame
def update(frame):
    # Change arrow properties for each frame
    U_updated = U * frame
    V_updated = V * frame
    W_updated = W * frame
    quiver = ax.quiver(X, Y, Z, U_updated, V_updated, W_updated)
    return quiver

# Create the animation
animation = FuncAnimation(fig, update, frames=10, interval=200, blit=False)

# Set axes labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Display the animation
HTML(animation.to_html5_video())

Although this version produces no errors, each frame has all the old quivers on top of the new one. Is there any way to delete these old quivers?

Ivan Burbano
  • 239
  • 1
  • 3
  • 11

0 Answers0