You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib:如何实现多散点图(多粒子运动)动画效果?

多粒子运动动画实现指南

嘿,我来帮你搞定多粒子运动动画的问题!你已经能实现单个粒子的动画了,那扩展到多个粒子其实也没那么难,咱们一步步来~

先回顾单个粒子的完整实现

你之前的单个粒子代码大概是这样的(我帮你补全了update部分):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# 单个粒子的位置记录:shape (步数, 2)
positions = np.array([[2,2],[3,3],[4,4],[5,3],[4,2],[3,1]])

fig, ax = plt.subplots()
ax.set_xlim(0, 6)
ax.set_ylim(0, 6)
scatterplot = ax.scatter([], [], color='blue', s=50)

def init():
    scatterplot.set_offsets([[], []])
    return [scatterplot]

def update(frame):
    # 取出当前帧的粒子位置
    current_pos = positions[frame:frame+1, :]
    scatterplot.set_offsets(current_pos)
    return [scatterplot]

# 创建动画
ani = animation.FuncAnimation(fig, update, init_func=init, frames=len(positions), interval=200, blit=True)
plt.show()

扩展到多粒子的核心改动

要实现多粒子动画,关键是调整位置数组的结构和update函数的逻辑:

1. 构造多粒子的位置数据

假设你有3个粒子,每个粒子有6步位置,那么positions数组应该是三维数组,形状为(步数, 粒子数, 2)。比如:

# 示例:3个粒子,6步位置
positions = np.array([
    [[2,2], [1,3], [5,1]],  # 第0步所有粒子的位置
    [[3,3], [2,4], [4,2]],  # 第1步
    [[4,4], [3,3], [3,3]],  # 第2步
    [[5,3], [2,2], [2,4]],  # 第3步
    [[4,2], [1,1], [1,3]],  # 第4步
    [[3,1], [0,2], [2,2]]   # 第5步
])

2. 修改update函数

不需要改动init函数,只需要在update里取出当前帧所有粒子的位置即可:

def update(frame):
    # 取出当前帧所有粒子的位置:shape (粒子数, 2)
    current_positions = positions[frame, :, :]
    scatterplot.set_offsets(current_positions)
    return [scatterplot]

3. 完整的多粒子动画代码

把这些整合起来,完整代码如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# 多粒子位置数据:shape (步数, 粒子数, 2)
positions = np.array([
    [[2,2], [1,3], [5,1]],
    [[3,3], [2,4], [4,2]],
    [[4,4], [3,3], [3,3]],
    [[5,3], [2,2], [2,4]],
    [[4,2], [1,1], [1,3]],
    [[3,1], [0,2], [2,2]]
])

fig, ax = plt.subplots()
# 根据粒子位置范围设置坐标轴,避免粒子跑出视野
ax.set_xlim(positions[:, :, 0].min() - 1, positions[:, :, 0].max() + 1)
ax.set_ylim(positions[:, :, 1].min() - 1, positions[:, :, 1].max() + 1)

# 初始化散点图,可以给不同粒子设置不同颜色哦
scatterplot = ax.scatter([], [], c=['blue', 'red', 'green'], s=50)

def init():
    scatterplot.set_offsets([[], []])
    return [scatterplot]

def update(frame):
    current_pos = positions[frame, :, :]
    scatterplot.set_offsets(current_pos)
    return [scatterplot]

# 帧数等于位置数组的步数
ani = animation.FuncAnimation(fig, update, init_func=init, frames=positions.shape[0], interval=200, blit=True)

plt.show()

额外小技巧

  • 如果想给每个粒子添加轨迹,可以在update函数里把当前帧之前的所有位置都画出来,比如用ax.plot()记录每个粒子的历史路径;
  • 调整interval参数可以改变动画速度,数值越小速度越快;
  • 可以给粒子设置不同的大小、颜色,让区分更明显。

内容的提问来源于stack exchange,提问作者Ingvild

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:49:08