做一个动态的心形图
时间: 2025-07-21 16:54:36 浏览: 1
<think>我们将使用matplotlib和numpy来创建一个动态的心形图。动态效果将包括一个跳动的心形,并且颜色也会随时间变化。
步骤:
1. 导入必要的库:numpy, matplotlib, 以及matplotlib的动画模块。
2. 定义心形的参数方程。经典的心形线方程可以表示为:
x = 16 * (sin(t))^3
y = 13 * cos(t) - 5 * cos(2t) - 2 * cos(3t) - cos(4t)
3. 使用matplotlib的FuncAnimation来创建动画,每一帧改变心形的缩放比例以模拟跳动效果,同时改变颜色。
我们将创建一个函数update(frame),该函数将根据当前帧更新心形的缩放比例和颜色。
注意:为了有平滑的跳动效果,我们将使用正弦函数来控制缩放比例,这样就会有一个从大到小再变大的循环。
此外,我们还会改变心形的颜色,让它在红色到粉色之间渐变。
代码实现如下:</think># 动态心形图
下面是一个使用Python的Matplotlib库创建的动态心形图。这个动画展示了一个跳动的心形,颜色会随时间渐变,并且包含背景粒子效果。
```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.collections import PathCollection
import matplotlib.patches as patches
from matplotlib.path import Path
import random
import math
# 设置图形
plt.figure(figsize=(10, 8), facecolor='black')
ax = plt.axes()
ax.set_facecolor('black')
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
ax.set_aspect('equal')
ax.axis('off') # 隐藏坐标轴
# 心形参数方程
def heart_x(t):
return 16 * np.sin(t) ** 3
def heart_y(t):
return 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
# 创建心形路径
t = np.linspace(0, 2 * np.pi, 300)
x = heart_x(t) / 16
y = heart_y(t) / 16
# 创建心形补丁
heart_path = np.column_stack([x, y])
heart = patches.PathPatch(Path(heart_path), facecolor='red', edgecolor='pink', lw=2, alpha=0.9)
ax.add_patch(heart)
# 创建粒子
class Particle:
def __init__(self):
self.reset()
def reset(self):
# 随机位置
angle = random.uniform(0, 2 * math.pi)
radius = random.uniform(0.5, 2.0)
self.x = radius * math.cos(angle)
self.y = radius * math.sin(angle)
# 随机速度
speed = random.uniform(0.005, 0.02)
angle = random.uniform(0, 2 * math.pi)
self.vx = speed * math.cos(angle)
self.vy = speed * math.sin(angle)
# 随机大小和颜色
self.size = random.uniform(5, 50)
self.color = (random.random(), random.random(), random.random(), random.uniform(0.3, 0.8))
# 生命周期
self.life = random.randint(50, 150)
self.max_life = self.life
def update(self):
# 移动粒子
self.x += self.vx
self.y += self.vy
# 减少生命周期
self.life -= 1
# 更新透明度
self.color = (self.color[0], self.color[1], self.color[2],
self.color[3] * (self.life / self.max_life))
# 如果生命结束,重置粒子
if self.life <= 0:
self.reset()
# 检查是否在边界内,否则重置
if abs(self.x) > 3 or abs(self.y) > 3:
self.reset()
return (self.x, self.y, self.size, self.color)
# 创建粒子系统
particles = [Particle() for _ in range(100)]
scatter = ax.scatter([], [], s=[], c=[], alpha=0.6)
# 动画更新函数
def update(frame):
global heart
# 更新心形大小 (跳动效果)
scale = 1 + 0.1 * np.sin(frame * 0.1)
new_heart_path = heart_path * scale
heart.set_path(Path(new_heart_path))
# 更新心形颜色 (渐变效果)
r = (np.sin(frame * 0.05) + 1) / 2
g = (np.sin(frame * 0.05 + 2) + 1) / 4
b = (np.sin(frame * 0.05 + 4) + 1) / 2
heart.set_facecolor((r, g, b))
# 更新心形边框颜色
border_r = min(1.0, r + 0.3)
border_g = min(1.0, g + 0.3)
border_b = min(1.0, b + 0.3)
heart.set_edgecolor((border_r, border_g, border_b))
# 更新粒子
particle_data = [p.update() for p in particles]
xs, ys, sizes, colors = zip(*particle_data)
# 更新散点图
scatter.set_offsets(np.column_stack([xs, ys]))
scatter.set_sizes(sizes)
scatter.set_color(colors)
# 添加标题
plt.title('❤️ 跳动的心 ❤️', color='white', fontsize=20, fontweight='bold', pad=20)
return heart, scatter
# 创建动画
ani = FuncAnimation(plt.gcf(), update, frames=200, interval=50, blit=True)
plt.tight_layout()
plt.show()
```
## 代码功能说明
这个动态心形图包含以下特点:
1. **跳动的心形**:
- 使用参数方程绘制经典心形曲线
- 心形会随时间规律性地缩放,模拟心跳效果
- 心形颜色会随时间平滑渐变
2. **彩色粒子效果**:
- 心形周围有100个随机运动的粒子
- 每个粒子有随机的大小、颜色和运动轨迹
- 粒子有生命周期,会逐渐消失并重生
3. **视觉效果**:
- 黑色背景增强心形和粒子的视觉效果
- 心形边框颜色与填充色协调
- 粒子使用半透明效果,增强层次感
## 运行要求
运行此代码需要安装以下Python库:
- Matplotlib
- NumPy
可以使用以下命令安装:
```
pip install matplotlib numpy
```
## 自定义选项
你可以通过修改以下参数来自定义动画效果:
1. 修改`heart.set_facecolor`和`heart.set_edgecolor`中的颜色值
2. 调整`scale`公式中的系数来改变心跳幅度
3. 修改`particles`列表的大小来增加或减少粒子数量
4. 调整`Particle`类中的参数来控制粒子行为
## 数学原理
心形曲线使用参数方程:
```
x = 16 * sin³(t)
y = 13 * cos(t) - 5 * cos(2t) - 2 * cos(3t) - cos(4t)
```
其中t在0到2π之间变化。
这个方程产生一个经典的心形曲线,我们通过缩放参数来实现心跳效果。
阅读全文
相关推荐

















