包作工 发表于 2023-1-6 17:05:35

ChatGPT用Python做的旋转动画代码

本帖最后由 包作工 于 2023-1-6 17:15 编辑

ChatGPT用Python做的旋转动画代码。没有测试过。如下;

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

# Set up the figure and axes
fig, ax = plt.subplots()

# Set up the parameters of the animation
num_frames = 360# Number of frames in the animation
fps = 60          # Frame rate (frames per second)

# Create a function that returns the coordinates of the points to be plotted
def get_points(angle):
x = np.cos(np.radians(angle))
y = np.sin(np.radians(angle))
return x, y

# Create the init function that sets up the plot
def init():
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
line, = ax.plot([], [], "o-")# Create the plot object
return line,

# Create the update function that updates the plot for each frame
def update(i):
x, y = get_points(i)# Get the coordinates for the current frame
line.set_data(x, y)   # Update the plot data
return line,

# Create the animation object
anim = animation.FuncAnimation(
    fig, update, frames=num_frames, init_func=init, interval=1000/fps
)

# Show the animation
plt.show()
页: [1]
查看完整版本: ChatGPT用Python做的旋转动画代码