How to plot an animated image matrix in matplotlib?

MatplotlibPythonData Visualization

To plot an animated image matrix in matplotlib, we can take the following steps

Steps

• Set the figure size and adjust the padding between and around the subplots.

• Create a figure and a set of subplots.

• Make an animation by repeatedly calling a function *update*.

• Inside the update method, create a 6×6 dimension of matrix and display the data as an image, i.e., on a 2D regular raster.

• Turn off the axes using set_axis_off().

• To display the figure, use Show() method.

Example

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

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()

def update(i):
im_normed = np.random.rand(6, 6)
ax.imshow(im_normed)
ax.set_axis_off()

anim = FuncAnimation(fig, update, frames=20, interval=50)

plt.show()


Output

It will produce the following output −

Published on 11-Oct-2021 08:20:14