【问题标题】:Matplotlib: how to animate pcolormesh with large data setMatplotlib:如何使用大型数据集为 pcolormesh 设置动画
【发布时间】:2018-08-10 07:58:21
【问题描述】:

我正在使用matplotlib.pyplot 为一些数组数据设置动画。数据采用强度图的形式,因此我有一个由 x 和 y 位置组成的网格,以及与这些位置相关联的值。

困难在于我不能简单地更新强度数据,因为 x 和 y 位置也会发生变化。

例如,我可以得到这样的工作,但它需要首先有一个超定的 x 和 y 网格,它将覆盖整个范围:

cax = ax.pcolormesh(x, y, G[:-1, :-1, 0],
                    vmin=-1, vmax=1, cmap='Blues')
fig.colorbar(cax)

def animate(i):
     cax.set_array(G[:-1, :-1, i].flatten())

这行得通,但我最终得到了一个相当大的强度数组,其中大部分都是零。

我找到了一个示例here,它允许更改 x 和 y 值。这是修改后的 MWE:

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

fig2 = plt.figure()

x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(15):
    x = np.arange(-9+add, 10+add)
    y = np.arange(-9+add, 10+add)
    x, y = np.meshgrid(x, y)
    ims.append((plt.pcolormesh(x, y, base + add, norm=plt.Normalize(0, 30)),))

im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
                                   blit=True)
plt.show()

这里的问题是双重的。首先,我有大约 3000 帧,所以列表 ims 变得无法管理。其次,我怎样才能让数据在帧之间清除而不是一次显示每一帧?也许有更好的方法?

奖励:使用滑块可以替代动画。我以前在这些类型的数据上使用过Slider,但只是通过初始化一个巨大的 x 和 y 网格。

感谢您的帮助!抱歉,如果我没有使用正确的标签。

【问题讨论】:

  • 标准 img 动画和通过手动设置显示值并根据实际数据的变化添加动画刻度和刻度标签怎么样?这会有帮助吗?
  • @mikuszefski 不完全是。对于我想要完成的事情,我不希望显示的 x 和 y 限制每次都改变。我想避免过度确定网格方法以节省计算时间和资源。改变每一帧的情节刻度将向后退一步。就目前而言,过度确定的网格似乎是最好的选择,尽管它并不优雅。
  • 嗯,我的印象是性能是一个问题,并且查看 cmets here,似乎支持我之前评论的想法,即使用 imshow()。不过,这需要手动设置刻度标签,而我认为这在性能方面应该是可以的。
  • ...除非您的 xy 不等距。

标签: python-2.7 animation matplotlib large-data


【解决方案1】:

我可能会误解这里的问题,但在这里使用FuncAnimation 似乎更合适。

带位传送

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

fig, ax = plt.subplots()

x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)

def animate(i):
    x = np.arange(-9+i, 10+i)
    y = np.arange(-9+i, 10+i)
    x, y = np.meshgrid(x, y)
    pc = ax.pcolormesh(x, y, base + i, norm=plt.Normalize(0, 30))
    return pc,

ax.axis([-9,30,-9,30])
im_ani = animation.FuncAnimation(fig, animate, frames=30, interval=50, 
                                 repeat_delay=3000, blit=True)
plt.show()

没有blitting

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

fig, ax = plt.subplots()

x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)


store=[]
def animate(i):
    x = np.arange(-9+i, 10+i)
    y = np.arange(-9+i, 10+i)
    x, y = np.meshgrid(x, y)
    if store:
        store[0].remove()
        del store[0]
    pc = ax.pcolormesh(x, y, base + i, norm=plt.Normalize(0, 30))
    store.append(pc)


ax.axis([-9,30,-9,30])
im_ani = animation.FuncAnimation(fig, animate, frames=30, interval=50, 
                                 repeat_delay=3000)
plt.show()

【讨论】:

  • 快到了。使用“blit=True”,什么都不会显示,但没有它,一切都会显示。添加“ax.clear()”是可行的,但是每次都必须调用它(并重置所有其他轴选项)是荒谬的。我可能只需要满足于过度确定的网格解决方案。感谢您的输入
  • 我不知道为什么blit=True 不适合你;在任何情况下,我都添加了另一种解决方案而不使用 blitting。
猜你喜欢
  • 2014-02-24
  • 2020-06-28
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多