简体   繁体   English

Matplotlib:如何使用大数据集为pcolormesh设置动画

[英]Matplotlib: how to animate pcolormesh with large data set

I am using matplotlib.pyplot to animate some array data. 我正在使用matplotlib.pyplot为某些数组数据设置动画。 The data is in the form of an intensity map, so I have a mesh of x and y locations, and a value associated with those locations. 数据采用强度图的形式,因此我具有x和y位置的网格,以及与这些位置关联的值。

The difficulty is that I cannot simply update the intensity data because the x and y locations change as well. 困难在于我不能简单地更新强度数据,因为x和y位置也会发生变化。

For example, I can get something like this work, but it requires having an over-determined x and y grid first that will cover the entire range: 例如,我可以得到类似的工作,但是它需要首先确定一个覆盖整个范围的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())

This works, but I end up with a fairly large intensity array filled mostly with zeros. 这行得通,但是我最终得到了一个很大的强度数组,其中大部分填充了零。

I have found an example here that allows the x and y values to be changed. 我在这里找到了一个示例,该示例允许更改x和y值。 Here is a modified MWE: 这是修改后的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()

The issue here is two-fold. 这里的问题有两个方面。 First, I have about 3000 frames, so the list ims becomes unmanageable. 首先,我大约有3000帧,因此列表ims变得难以管理。 Secondly, how can I get the data to clear between frames and not show every frame all at once? 其次,如何才能在帧之间清除数据,而不一次显示所有帧? Perhaps there's a better way altogether? 也许总有更好的方法?

Bonus: using a slider could be an alternative to an animation. 奖励:使用滑块可以替代动画。 I've used Slider on these types of data before, but only by initializing a huge x and y grid. 我以前在这些类型的数据上使用过Slider ,但是仅通过初始化巨大的x和y网格即可。

Thanks for the help! 谢谢您的帮助! Apologies if I'm not using the proper tags. 抱歉,如果我没有使用正确的标签。

I may misunderstand the problem here, but using a FuncAnimation seems more appropriate here. 我可能在这里误解了问题,但是在这里使用FuncAnimation似乎更合适。

With 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)

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()

Without 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM