【问题标题】:Matplotlib: Animate multiple scatter plotsMatplotlib:动画多个散点图
【发布时间】:2018-04-16 00:03:28
【问题描述】:

我正在尝试创建一个显示多个粒子移动的动画。

如果我有一个带有一个数组的粒子,它在动画的每个步骤中给出了该粒子的位置,我可以让它工作(主要归功于我在 stackoverflow 上找到的其他答案的广泛帮助)。

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


positions = np.array([[2,2],[3,3],[4,4]])


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

def update(i, scatterplot, positions):
    scatterplot.set_offsets(positions[i])
    return [scatterplot]

fig = plt.figure()

scatterplot = plt.scatter([], [], s=100)
plt.xlim(0,5)
plt.ylim(0,5)
anim = animation.FuncAnimation(
fig, update, init_func=init, fargs=(scatterplot, positions), interval=1000, frames=3,
blit=True, repeat=True)
plt.show()

但我不知道如何在同一个动画中添加更多粒子。 假设我想添加第二个带有位置的粒子

positions2 = np.array([[2,1][3,2][4,3]])

让它在同一个散点图中移动,我该如何管理?

我是 matplotlib 新手,一直在疯狂搜索无济于事,非常感谢您的帮助:)

编辑: 我最终想通了,只是正确格式化数据的问题。

positions = np.array([[[2,2],[2,1]],[[3,3],[3,2]],[[4,4],[4,3]]])

如果数组包含第一步中的所有位置,那么第二步中的所有位置等都有效。 我更希望获得一个颜色 pr 移动点,以跟踪它们,但至少它现在可以工作。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我最终想通了,只是正确格式化数据的问题。

    positions = np.array([[[2,2],[2,1]],[[3,3],[3,2]],[[4,4],[4,3]]])
    

    如果数组包含第一步中的所有位置,那么第二步中的所有位置等都有效。 我更希望获得一个颜色 pr 移动点,以跟踪它们,但至少它现在可以工作。

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 2012-07-07
      • 2020-09-21
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      相关资源
      最近更新 更多