• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Animated heatmap with Seaborn and matplotlib


New Coder
Hello everyone !

I’m trying to animate a heatmap for data visualization.
(Just to clarify, I am a beginner in coding)

The heatmap is generated from a matrix where cells situated at the center of the map take a +1 value at each iteration. I don’t use numpy for the matrix but comprehension list. Heatmaps are generated with Seaborn module.

The heatmap is generated correctly and the matrix values change well. However, the animation does not start. I get a frozen image as if I had only generated a heat map without animation.

Here is the code :

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
#Definition of the initial matrix
def random_bed(p, n):
… states=[[0]*n for _ in range(n)]
… A=[(3,3),(3,4),(3,5),(3,6),(4,3),(4,4),(4,5),(4,6),(5,3),(5,4),(5,5),(5,6),(6,3),(6,4),(6,5),(6,6)]
… for (i,j) in A:
… states[j]=1
… return states
states=random_bed(p, n)
#function which aims to animate the matrix and increase the values of the center
def animate(i):
… data = states
… A=[(3,3),(3,4),(3,5),(3,6),(4,3),(4,4),(4,5),(4,6),(5,3),(5,4),(5,5),(5,6),(6,3),(6,4),(6,5),(6,6)]
… for (i,j) in A:
… states[j]=states[j]+1
… return states
… sns.heatmap(data, vmax=100)
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate, frames=20, repeat=False)

Here is an example of the output (with no animation):

After many unsuccessful attempts I really can't find the solution..

Does anyone have an idea of my mistake?

Thank you very much for taking the time to read me.
Have a great day !

Top Bottom