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

效果

相关推荐
l木本I7 分钟前
大模型低秩微调技术 LoRA 深度解析与实践
python·深度学习·自然语言处理·lstm·transformer
哆啦A梦的口袋呀10 分钟前
基于Python学习《Head First设计模式》第七章 适配器和外观模式
python·学习·设计模式
十月狐狸13 分钟前
Python字符串进化史:从青涩到成熟的蜕变
python
狐凄1 小时前
Python实例题:Python计算线性代数
开发语言·python·线性代数
西猫雷婶1 小时前
pytorch基本运算-导数和f-string
人工智能·pytorch·python
述雾学java1 小时前
深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
人工智能·pytorch·python
要努力啊啊啊1 小时前
使用 Python + SQLAlchemy 创建知识库数据库(SQLite)—— 构建本地知识库系统的基础《一》
数据库·人工智能·python·深度学习·自然语言处理·sqlite
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
凤头百灵鸟3 小时前
Python语法基础篇(包含类型转换、拷贝、可变对象/不可变对象,函数,拆包,异常,模块,闭包,装饰器)
python
多多*4 小时前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua