Reputation: 357
I want to visualize a set of arrays overtime (with some pause in between) using matplotlib. What I have so far is the visualization for one array but I don't know how to make it like an animation. This is the code I have so far which creates a list of arrays and successfully visualizes the first array in the list.
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
plt.style.use('classic')
a = []
for i in xrange(5):
temp = np.zeros((5,5))
temp[i, i] = 10
a.append(temp)
fig, ax = plt.subplots(5, 5, figsize=(5, 5))
fig.subplots_adjust(hspace=0, wspace=0)
for i in range(5):
for j in range(5):
ax[i, j].xaxis.set_major_locator(plt.NullLocator())
ax[i, j].yaxis.set_major_locator(plt.NullLocator())
if a[0][i,j] == 10:
ax[i, j].imshow(Image.open('A.png'), cmap="bone")
else:
ax[i, j].imshow(Image.open('B.png'), cmap="bone")
plt.show()
How can I visualize all arrays of the list like an animation?
Upvotes: 0
Views: 132
Reputation: 411
You'll need to import the animation module and define a function that changes the frame to show each image you captured.
from matplotlib.animation import FuncAnimation
def update(i):
label = 'timestep {0}'.format(i)
//Insert the data the frame here Eg:ax.imgshow(Image.open('A.png'), cmap="bone")
ax.set_xlabel(label)
return line, ax
FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200)
plt.show()
You can follow this handy guide:
https://eli.thegreenplace.net/2016/drawing-animated-gifs-with-matplotlib/
Upvotes: 1