How to animate a heat map or a seabed correlation matrix?

I am relatively new to Python (from Matlab). As one project, I am trying to create an animated graph of the correlation matrix over time. To make the stories good, I try the horn. I tried my best to make the animation at all (there were problems with the Matplotlib backend on Mac), but now with this code from the Internet a very simple animation works:

import numpy as np from matplotlib import pyplot as plt from matplotlib import animation nx = 50 ny = 50 fig = plt.figure() data = np.random.rand(nx, ny) im = plt.imshow(data) def init(): im.set_data(np.zeros((nx, ny))) def animate(i): #xi = i // ny #yi = i % ny data = np.random.rand(nx, ny) im.set_data(data) return im anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, repeat = False) 

I tried to adapt this to seaborn , but to no avail. It seems that offshore works work in the areas, and it was much more difficult to revive them. The best I got once was a kind of recursive plot where seaborn.heatmaps were seaborn.heatmaps on top of each other. In addition, the im.set_data method was not available.

Any suggestions are much appreciated.

+9
source share
2 answers

I replaced plt.imshow (casting via set_data did not work) with seaborn.heatmap .

 fig = plt.figure() data = np.random.rand(nx, ny) sns.heatmap(data, vmax=.8, square=True) def init(): sns.heatmap(np.zeros((nx, ny)), vmax=.8, square=True) def animate(i): data = np.random.rand(nx, ny) sns.heatmap(data, vmax=.8, square=True) anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False) 

This creates a recursive plot that I struggled with.

+3
source

Here is a complete example (tested with Matplotlib 3.0.3).

 import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np import seaborn as sns def animate_heat_map(): fig = plt.figure() nx = ny = 20 data = np.random.rand(nx, ny) ax = sns.heatmap(data, vmin=0, vmax=1) def init(): plt.clf() ax = sns.heatmap(data, vmin=0, vmax=1) def animate(i): plt.clf() data = np.random.rand(nx, ny) ax = sns.heatmap(data, vmin=0, vmax=1) anim = animation.FuncAnimation(fig, animate, init_func=init, interval=1000) plt.show() if __name__ == "__main__": animate_heat_map() 
0
source

All Articles