五星红旗迎风飘扬

本文展示了如何使用Python的matplotlib库结合numpy创建一个动态的五角星旋转动画,通过FuncAnimation函数实现360度的连续旋转并展示在图表上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值