0

I would like to plot an animated heatmap from a group of DataFrames (for example saved in a dictionary), either as gif or a movie.

For example, say I have the following collection of DFs. I can display all of them one after the other. But I would like to have them all being shown in the same figure in the same way as a GIF is shown (a loop of the heatmaps).

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

dataframe_collection = {}

for i in range(5):
    dataframe_collection[i] = pd.DataFrame(np.random.random((5,5)))

    # Here within the same loop just for brevity
    sns.heatmap(dataframe_collection[i])
    plt.show()

1 Answers1

0

The simplest way is to first create separate png images, and then use a software such as ImageMagick to convert them to an animated gif.

Example to create the png's:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

dataframe_collection = {}
for i in range(5):
    dataframe_collection[i] = pd.DataFrame(np.random.random((5,5)))
    #plt.pcolor(dataframe_collection[i])
    sns.heatmap(dataframe_collection[i])
    plt.gca().set_ylim(0, len(dataframe_collection[i])) #avoiding problem with axes
    plt.axis('off')
    plt.tight_layout()
    plt.savefig(f'dataframe_{i}.png')

After installing ImageMagick the following shell command creates a gif. If the defaults are not satisfying, use the docs to explore the many options.

convert.exe -delay 20 -loop 0 dataframe_*.png dataframes.gif

See also this post about creating animations and an animated gif inside matplotlib.

Note that Seaborn's heatmap also has some features such as sns.heatmap(dataframe_collection[i], annot=True).

If you're unable to use ImageMagick, you could show a video by quickly displaying single png files, simulating a video. This and this post contain more explanations and example code. Especially the second part of this answer looks promising.

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Is there a way to avoid using an external software (ImageMagick in this example)? – Tiago Teodoro Jan 05 '20 at 17:29
  • The solution mentioned in the linked post uses matplotlib's animation module to create the gifs. But this also only works via ImageMagick. And you'd have less options. – JohanC Jan 05 '20 at 17:34
  • Note that ImageMagick is a highly optimized and highly flexible open source package. Unfortunately, it hasn't a good python binding. But the command line is quite easy to use. Inside Python, you could do a system call with `import io; system('convert ....')`. Note that gifs are a very old format with only 256 colors and many trickery to compress animations. All the frames are needed together to create the gif. – JohanC Jan 05 '20 at 17:57
  • Hi, JohanC. Thanks for the help! I'm working on a server and they don't have ImageMagick installed (on my laptop it worked though). That's why I asked. I have of course seen many examples of animations using matplotlib. But I still cannot get something working with heatmaps. Those codes with a init() and update() function look confusing to me and I cannot translate that to using the heatmaps like in the question code. – Tiago Teodoro Jan 05 '20 at 18:05
  • You'd need a backend such as ImageMagick or ffmeg to create an animation file. Most web-services have both installed standardly, as they are used very frequently. ImageMagick is used for example when creating thumbnails. I think the "confusing" matplotlib's animation module won't save an animation file if you don't have ImageMagick nor ffmpeg installed. – JohanC Jan 05 '20 at 22:18