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

效果

相关推荐
陈天伟教授3 小时前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
2301_764441333 小时前
Aella Science Dataset Explorer 部署教程笔记
笔记·python·全文检索
爱笑的眼睛113 小时前
GraphQL:从数据查询到应用架构的范式演进
java·人工智能·python·ai
BoBoZz193 小时前
ExtractSelection 选择和提取数据集中的特定点,以及如何反转该选择
python·vtk·图形渲染·图形处理
liwulin05063 小时前
【PYTHON-YOLOV8N】如何自定义数据集
开发语言·python·yolo
木头左4 小时前
LSTM量化交易策略中时间序列预测的关键输入参数分析与Python实现
人工智能·python·lstm
电子硬件笔记4 小时前
Python语言编程导论第七章 数据结构
开发语言·数据结构·python
HyperAI超神经5 小时前
【vLLM 学习】Prithvi Geospatial Mae
人工智能·python·深度学习·学习·大语言模型·gpu·vllm
逻极5 小时前
Python MySQL防SQL注入实战:从字符串拼接的坑到参数化查询的救赎
python·mysql·安全·sql注入
赫凯5 小时前
【强化学习】第一章 强化学习初探
人工智能·python·强化学习