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

效果

相关推荐
im_AMBER3 小时前
学习日志19 python
python·学习
mortimer5 小时前
安装NVIDIA Parakeet时,我遇到的两个Pip“小插曲”
python·github
@昵称不存在6 小时前
Flask input 和datalist结合
后端·python·flask
赵英英俊6 小时前
Python day25
python
东林牧之6 小时前
Django+celery异步:拿来即用,可移植性高
后端·python·django
何双新7 小时前
基于Tornado的WebSocket实时聊天系统:从零到一构建与解析
python·websocket·tornado
AntBlack7 小时前
从小不学好 ,影刀 + ddddocr 实现图片验证码认证自动化
后端·python·计算机视觉
凪卄12138 小时前
图像预处理 二
人工智能·python·深度学习·计算机视觉·pycharm
巫婆理发2228 小时前
强化学习(第三课第三周)
python·机器学习·深度神经网络
seasonsyy8 小时前
1.安装anaconda详细步骤(含安装截图)
python·深度学习·环境配置