2

使用“contourf”方法时 ArtistAnimation 是否有效?我正在尝试构建模型预测的 MP4 动画。

我正在使用一种类似于 MetPy Monday 视频系列中显示的方法,在该视频系列中,您将绘图附加到一个空的艺术家数组(用于雷达图)。雷达/卫星图似乎成功地构建了动画,因为它们使用了“pcolormesh”,但对于我使用“contourf”的模型图来说,情况并非如此。

在 JupyterLab 中执行此代码时,

plt.rcParams['animation.html'] = 'jshtml'
anim = ArtistAnimation(fig, artists, interval=100, blit=False)
anim

我在堆栈跟踪的末尾看到了这一点:

AttributeError:“QuadContourSet”对象没有属性“set_visible”

stackoverflow 上的一个旧线程尝试解决此问题: https ://github.com/matplotlib/matplotlib/issues/6139

实施这些修复时,set_visible 属性错误不再出现,而是出现:

AttributeError:“QuadContourSet”对象没有属性“get_figure”

不知道从这里去哪里。如果有好消息,我会在地图上一次看到每个时间步的所有数据(因此数据检索是可以的),但是 ArtistAnimation 以及它如何与 contourf 方法一起工作。任何指导或有用的提示都会很棒!

4

1 回答 1

1

由于我不明白的原因, , 直接返回的对象contour不是QuadContourSet,Artist与许多其他绘图命令不同——这就是它没有set_visible方法的原因。经过一番挖掘,QuadContourSet有一个.collections属性是所有绘制的线条集合的列表,所以你想在这样的东西中使用它,它是从 matplotlib 的dynamic_image.py示例修改的:

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

fig, ax = plt.subplots()

def f(x, y):
    return np.sin(x) + np.cos(y)

x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)

artists = []
for i in range(60):
    X, Y = np.meshgrid(x, y)
    cs = ax.contour(X, Y, f(x + i * np.pi/15., y + i*np.pi/20))
    artists.append(cs.collections)

ani = animation.ArtistAnimation(fig, artists, interval=50, blit=True,
                                repeat_delay=1000)

plt.show()
于 2020-05-01T22:13:18.137 回答