I am using matplotlib.pyplot
to animate some array data. 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.
The difficulty is that I cannot simply update the intensity data because the x and y locations change as well.
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:
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. Here is a modified 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. 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.
Thanks for the help! Apologies if I'm not using the proper tags.