• 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.
    • 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

Axel

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
p=1
n=10
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()
sns.heatmap(states)
anim = animation.FuncAnimation(fig, animate, frames=20, repeat=False)
plt.show()


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

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 !

Axel MEYER
 
Top