I would like to add a colorbar to some data to make an animation.
However, I keep on creating new color bars in the figure and don't know how to remove them.
A reproducible example is:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import cm
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111, projection='3d', proj_type='ortho')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
data = [X, Y, Z]
def myPlot(ax, data):
surf = ax.plot_surface(*data, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
def anime(i, ax, data):
ax.cla()
data[0] += 0.1
myPlot(ax, data)
animation = FuncAnimation(
fig,
anime,
frames=range(len(X)),
fargs=(ax, data)
)
How can I keep only one colorbar in the animation?
Kind regards
