[PHP] 纯文本查看 复制代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 初始化画布
fig, ax = plt.subplots(figsize=(8, 5))
ax.set_xlim(0, 10)
ax.set_ylim(0, 6)
ax.axis('off')
# 初始化元素
sky = ax.fill_between([0, 10], [6, 6], [6, 6], color='skyblue') # 天空
sun = plt.Circle((8, 5), 0.6, color='gold', alpha=0.8) # 太阳
sea = ax.fill_between([], [], color='dodgerblue') # 海洋
wave_line, = ax.plot([], [], 'white', lw=2) # 海浪线
boat = plt.Polygon([[4,1.5], [5,1.5], [4.8,1.3]], color='saddlebrown') # 小船
cloud = plt.Circle((2, 5), 0.8, color='white', alpha=0.9) # 云朵
ax.add_patch(sun)
ax.add_patch(boat)
ax.add_patch(cloud)
# 动画参数
total_frames = 60 # 6秒*10帧/秒
wave_amplitude = 0.3
boat_speed = 0.08
cloud_speed = 0.05
def animate(frame):
# 海浪波动
x = np.linspace(0, 10, 100)
y_sea = 2 + wave_amplitude * np.sin(0.8*x + 0.1*frame)
sea[0].set_xy(np.column_stack([x, y_sea]))
wave_line.set_data(x, y_sea + 0.1)
# 小船移动
boat.set_xy([[4+boat_speed*frame,1.5],
[5+boat_speed*frame,1.5],
[4.8+boat_speed*frame,1.3]])
# 云朵飘动
cloud.center = (2 + cloud_speed*frame, 5 + 0.1*np.sin(0.1*frame))
# 阳光闪烁效果
sun.set_alpha(0.7 + 0.1*np.sin(0.3*frame))
return sea, wave_line, boat, cloud, sun
# 生成动画
ani = FuncAnimation(fig, animate, frames=total_frames, blit=True, interval=100)
ani.save('sea_scene.mp4', writer='ffmpeg', fps=10, dpi=100)
print("视频已生成:sea_scene.mp4")