画动态爱心(Python-matplotlib)

介绍

氵而已

由于用的是 AI,注释得非常清楚,自己改改也可以用

代码

python 复制代码
# -*- coding: utf-8 -*-
# Environment    PyCharm
# File_name   尝试1 |User    Pfolg
# 2024/11/05 22:45
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# 定义心形的参数方程
color = "#F9A6FF"
t = np.linspace(0, 2 * np.pi, 1000)
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)
# 设置轴的标签
fig, ax = plt.subplots()
ax.set_aspect('equal')  # 设置x和y轴的比例相同
ax.set_xlabel('埃克斯', fontproperties="SimHei", fontsize=16)
ax.set_ylabel('歪', fontproperties="SimHei", fontsize=16)
ax.set_title('', fontproperties='SimHei', fontsize=16)
# # 关闭坐标轴
ax.axis('off')
# 绘制初始心形
line, = ax.plot(x, y, color=color)
# 设置轴的范围
ax.set_xlim(min(x) - 5, max(x) + 5)
ax.set_ylim(min(y) - 5, max(y) + 5)
# 设置爱心中间文本
ax.text(0, 0, 'heart'.upper(), fontsize=20, ha='center', va='center', color=color)


# 定义一个缓动函数
def ease_in_out(t):
    return (np.sin(np.pi * t - np.pi / 2) + 1) / 2


# 初始化函数
def init():
    line.set_data([], [])
    return line,


# 动画函数
# def animate(i):
#     # 模拟跳动效果,通过改变心形的大小
#     scale = 1 + 0.3 * np.sin(2 * np.pi * i / 20)
#     line.set_data(x * scale, y * scale)
#     return line,
def animate(i):
    # 使用缓动函数来模拟更自然的跳动
    scale = 1 + 0.3 * ease_in_out(i / 20)
    line.set_data(x * scale, y * scale)
    return line,


# 设置视频的时长和帧率
T = 10  # 视频时长为2秒
fps = 30  # 帧率为30帧每秒

# 计算总帧数
frames = int(T * fps)
# 创建动画
ani = animation.FuncAnimation(
    fig, animate, init_func=init, frames=frames, interval=50, blit=True
)

# 显示动画
plt.show()

# 保存动画为GIF文件
# ani.save('sine_wave.gif', writer='imagemagick')

# 如果你安装了FFmpeg,也可以保存为MP4文件
ani.save('sine_wave.mp4', writer='ffmpeg', fps=20)

FFmpeghttps://ffmpeg.org/

效果

相关推荐
mit6.82411 小时前
py期中实验选题:实现天气预测
python·算法
Rolei_zl12 小时前
AIGC(生成式AI)试用 41 -- 程序(Python + OCR)-3
python·aigc
eybk12 小时前
使用Beeware开发文件浏览器获取Android15的文件权限
python
柒柒钏12 小时前
VSCode 终端配置与 Python 虚拟环境使用指南
ide·vscode·python
环己酮13 小时前
py数据科学学习笔记day4-空间数据统计分析与可视化(2)
python
q***482513 小时前
基于python语言的网页设计(手把手教你设计一个个人博客网站)
开发语言·python
qq_225891746613 小时前
基于Python+Django餐饮评论大数据分析与智能推荐系统 毕业论文
开发语言·后端·python·信息可视化·数据分析·django
FreakStudio14 小时前
串口协议解析实战:以 R60ABD1 雷达为例,详解 MicroPython 驱动中数据与业务逻辑的分离设计
python·单片机·pycharm·嵌入式·面向对象·硬件·电子diy
南山安14 小时前
让 LLM 与外界对话:使用 Function Calling 实现天气查询工具
人工智能·后端·python
用户120391129472614 小时前
打破信息壁垒:手把手教你实现DeepSeek大模型的天气查询功能
python·openai