Continously update a heatmap in Jupyter notebook

I have a 2D numpy array in a Jupyter notebook. The data is updated in a loop. For each circle in the loop, I would like to update the heatmap once in place(!).

My failure of finding a simple example for that may be caused by lack of google skills, or I am using the wrong tool here. Everything I found is rather old or about making an animation (which I don’t need).

Thanks!

If you use the ipympl backend you can display a single plot and keep updating it as you wish.

First cell in notebook:

%matplotlib ipympl

import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng()
arr = rng.uniform(size=(5, 5))

fig, ax = plt.subplots()
im = ax.imshow(arr)

This will display the Matplotlib figure. In the next cell generate some different data for the image and call set_data on the Matplotlib artist that was returned from the imshow call:

arr = rng.uniform(size=(5, 5))
im.set_data(arr)

The plot will be updated in-place.

You can repeat the second cell as many times as you wish and each time the plot will be updated.