I am trying to build an animated heatmap using celluloid. The x & y axis and color scale are the same but my code returns the weird output below.
My code uses seaborn, numpy, pandas, and celluloid and is simplified below:
from celluloid import Camera
## Set up celluloid
fig = plt.figure(figsize=(12, 9))
camera = Camera(fig)
## Loop to create figures
for item in range(len(df)):
row = df.iloc[item]
row = np.array(list(row))
## Create df from row
shape = (8,12)
df_row = pd.DataFrame(row.reshape(shape))
## Build seaborn heatmap
ax = sns.heatmap(df_row, cmap="Greys", annot=False, vmin=0, vmax=1)
ax.set_title(item)
ax.xaxis.tick_top()
for tick in ax.get_yticklabels():
tick.set_rotation(0)
## Snap Celluloid
camera.snap()
anim = camera.animate(interval=500)
anim.save("animation.mp4")

The problem is that seaborn constantly creates a new colorbar. To solve it, a fixed
axfor the colorbar needs to be created at the start of the code.Here is the general setup, using
celluloid'sCamera. If you leave outcbar_ax=cbar_axyou'll see the strange behavior of an endless caravan of colorbars.The critical changes to your code would be:
fig = plt.figure(...)byfig, (ax, cbar_ax) = plt.subplots(...)sns.heatmapwithax=ax, cbar_ax=cbar_ax