How to plot an animated image matrix in matplotlib?


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 −

Updated on: 11-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements