import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation# 设置画布尺寸和五星红旗的比例
width = 30
height = 20
scale = 2# 创建画布和子图
fig, ax = plt.subplots(figsize=(width, height))# 设置坐标轴范围
ax.set_xlim(0, width)
ax.set_ylim(0, height)# 绘制红色背景
background = plt.Rectangle((0, 0), width, height, color='red')
ax.add_patch(background)# 定义五角星的坐标
star_points = np.array([
[3, 12], [9, 12], [11, 16], [13, 12], [19, 12], [15, 9], [17, 5], [11, 7], [5, 5], [7, 9]
])# 创建空的五角星
star = plt.Polygon(star_points, closed=True, color='yellow')
ax.add_patch(star)# 动画更新函数
def update(frame):
# 旋转五角星
angle = frame * np.pi / 180 # 将角度转换为弧度
rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
rotated_star_points = star_points.dot(rotation_matrix) * scale + np.array([10, 5])
star.set_xy(rotated_star_points)
return star,# 创建动画
ani = animation.FuncAnimation(fig, update, frames=360, interval=50, blit=True)# 显示动画
plt.axis('off')
plt.show()