3

I trying to make HTML(anim.to_html5_video) animation work in jupyter with seaborn heatmap.

  • First, I get working working samples from documentation, and make "pure matplotlib" image map animated example, it worked, with small problem ("parasite output" in animation cell)
  • Then, I tried to make it work with seaborn.heatmap… but failed. Animation looks like "infinite mirror" — obviously something wrong with matplotlib axes/plot composition, but I can't get it.

Common initialization cell:

import pandas as pd
import seaborn as sns
import numpy as np
%matplotlib inline
#%matplotlib notebook # Tried both, not needed for animation.
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML

Animation worked, but "unwanted static output image exists":

fig, ax = plt.subplots()
nx = 50
ny = 50

line2d, = ax.plot([], [], lw=2)

def init():
    line2d.set_data([], [])
    ax.imshow(np.zeros((nx, ny)))
    return (line2d,)

def animate(i):
    data = np.random.rand(nx, ny)
    ax.set_title('i: ' + str(i))    
    ax.imshow(data)
    return (line2d,)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=10, interval=1000, blit=False)

HTML(anim.to_html5_video())

So, looks that all OK with my jupyter setup (packages, ffmpeg, etc).

But, I cannot get how to make it with seaborn.heatmap:

fig, ax = plt.subplots()
nx = 50
ny = 50

line2d, = ax.plot([], [], lw=2)

ax_global = ax

def init_heatmap():
    line2d.set_data([], [])
    sns.heatmap(np.zeros((nx, ny)), ax=ax_global)
    return (line2d,)


def animate_heatmap(i):
    data = np.random.rand(nx, ny)
    sns.heatmap(data, ax=ax_global)
    ax.set_title('Frame: ' + str(i))    

    return (line2d,)

anim = animation.FuncAnimation(fig, animate_heatmap, init_func=init_heatmap,
                              frames=10, interval=1000, blit=True)

HTML(anim.to_html5_video())    

Both samples ready to test on github

Of course, I want to see animation with random map and "stable heat-axes" but get this https://vimeo.com/298786185/

Rohan Nadagouda
  • 462
  • 7
  • 18
belonesox
  • 121
  • 1
  • 7

1 Answers1

3

You can toggle the "colorbar". From the Seaborn.heatmap documentation, you need to change sns.heatmap(data, ax=ax_global) to sns.heatmap(data, ax=ax_global, cbar=False) and also do the same inside the init_heatmap().

mkahr
  • 31
  • 2