0

我有一个 JupyterLab 笔记本(v1.1.4),我试图在其中制作散点图的动画。我尝试使用 matplotlib 示例中的雨滴示例,它运行良好,但我无法让我的工作。

这是一个 MWE

%matplotlib widget
import random
import matplotlib.pyplot as plt
import matplotlib.animation as anim

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
p1, p2 = [], []

points = 10
for member in range(points):
    p1.append(random.random())
    p2.append(random.random())
scat = ax.scatter(p1,p2)

def animator(i):
    for idx in range(points):
        p1[idx] = random.random()
        p2[idx] = random.random()
    scat.set_offsets([p1, p2])

animation = anim.FuncAnimation(fig, animator, 50, interval=100)

它简要地显示了第一帧,但是一旦动画启动,我就会得到一个空白的图形。该小部件仍然存在,但其中没有任何内容,甚至没有轴框架。

4

1 回答 1

0

我尝试在交互式会话而不是笔记本中运行它。我收到以下错误:

ValueError:“顶点”必须是形状为 Nx2 的二维列表或数组

原来 [p1,p2] 列表需要转置。我将此列表转换为一个 numpy 数组并将其转置,瞧。

于 2020-07-03T20:43:07.230 回答