0

I want to make gif animation with FuncAnimation of matplotlib.

Here is the code.

However the output image contains multiple colorbars like this(link)

Does anyone know how to fix this problem ?

If I set

plt.colorbar() 

just after or before

ani = animation.FuncAnimation(fig, update, interval=100 ,frames=400)

, it returns RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

If I subsitute plt.colorbar() with if(k==0): plt.colorbar(), image contains same 2 colorbars.

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image
import moviepy.editor as edit
from mpl_toolkits.axes_grid1 import make_axes_locatable
def update(k):
    plt.cla() # clear axis

  #updating Val...

    plt.contourf(X0, Y0, Val, cmap = "bwr")
    plt.colorbar()
    plt.axes().set_aspect("equal")
    plt.title("title-name")

fig = plt.figure()
ani = animation.FuncAnimation(fig, update, interval=100 ,frames=400)
ani.save("out.gif", writer='imagemagick')
Koreyuki
  • 75
  • 7

1 Answers1

0

I add

plt.colorbar(plt.contourf(X0,Y0,charge,cmap="bwr",levels=np.linspace(-1,1,100,endpoint=True)))

just after

fig=plt.figure()

and it works successfully. This method is adopted from Mr. ImportanceOfBeingErnest's answer(Maintaining one colorbar for maptlotlib FuncAnimation)

Koreyuki
  • 75
  • 7