import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# Create a figure and a 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Define the time points
t = np.linspace(0, 2*np.pi, 100)
# Define the heart shape equation
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
# Initialize the plot
heart, = ax.plot([], [], [], 'r-', animated=True)
# Set up the axis labels and limits
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.set_zlim(-20, 20)
# Define the update function for the animation
def update(frame):
# Clear the previous frame
ax.cla()
# Compute the scaling factor based on the frame number
scale = 1 + np.sin(frame * 0.1)
# Compute the new x, y, and z coordinates
x_scaled = scale * x
y_scaled = scale * y
z = np.zeros_like(x)
# Update the plot data
heart.set_data(x_scaled, y_scaled)
heart.set_3d_properties(z)
# Set up the axis labels and limits
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.set_zlim(-20, 20)
return heart,
# Create the animation
ani = animation.FuncAnimation(fig, update, frames=100, interval=50, blit=True)
# Show the animation
plt.show()
Comments
Post a Comment