画动态爱心(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/

效果

相关推荐
可信计算20 小时前
【算法随想】一种基于“视觉表征图”拓扑变化的NLP序列预测新范式
人工智能·笔记·python·算法·自然语言处理
张广涛20 小时前
【无标题】
python
爱笑的眼睛1120 小时前
超越剪枝与量化:下一代AI模型压缩工具的技术演进与实践
java·人工智能·python·ai
yaoh.wang20 小时前
力扣(LeetCode) 14: 最长公共前缀 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
reasonsummer21 小时前
【办公类-18-07】20251215(Python)“口腔检查涂氟信息”批量生成打印(区名、学号、姓名、学校、班级、身份证、户籍、性别、民族)
开发语言·python
Rabbit_QL21 小时前
【PyTorch】detach:从计算图中切断梯度的原理与实践
人工智能·pytorch·python
晨曦54321021 小时前
MySQL与Python数学函数大比拼
python·mysql
一个无名的炼丹师21 小时前
【硬核实战】Python处理多源异构文档:从读取到智能信息提取的统一框架深度剖析
开发语言·python
Mr_Xuhhh21 小时前
JAVA期末重点
java·开发语言·python
测试-鹏哥21 小时前
全新驾驶舱功能助力ITP测试平台腾飞
python·测试工具·压力测试