I need to generate an animating Heatmap but not working?

You are in the right direction generating the frames that will compose the animation. However, the trick of sequentially throwing single pictures to the screen won’t do the trick (:melting_face: Matlab flashbacks), you’ll need to throw them to a video encoder. Here is an example using the matplotlib.animation module:

matplotlibanimate

Code:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
import streamlit as st
from itertools import cycle

## Generate some data
randomdata = np.random.random(size=(5,3,3))
harvest_cycle = cycle([i for i in randomdata])
harvest = next(harvest_cycle)

## Initialize figure
fig,ax = plt.subplots(figsize=[4,4])
ax.set_aspect('equal')
heatmap = ax.imshow(harvest)

st.markdown("## :camera: Initial condition")
st.pyplot(fig)

## Animate 
def update(frame_number): 
    ax.cla() ## Clears the data passed to the axes
    ##  This is important so the figure does not store data
    ##  of every single frame and slows everything down
    
    ax.imshow(next(harvest_cycle))
    ax.set_title(f"Frame: {frame_number}",fontsize=24)

animation = FuncAnimation(fig,update,frames=50)

## Saved mp4 video
animation.save("movie.mp4")

## Keep jshmtl representation
animjs = animation.to_jshtml()

st.markdown("## :movie_camera: Enconded MP4 movie")
st.video("movie.mp4")

st.markdown("## :control_knobs: Using `jshtml` from matplotlib")
st.components.v1.html(animjs,height=600)
3 Likes