Below is my exemplary animation with sub-plots. I have two colorbars: local and global. I found a way to update the global colorbar during animation. However, I can't find a way to update the local one.
In the current implementation, in every iteration I get an extra colorbar (see attached). Given the code structure, is there a way to clear/update it?
import numpy as np
from scipy import interpolate
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import math
from scipy.interpolate import RectBivariateSpline
import matplotlib.animation as animation
# Animation with subplots
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(15,15), subplot_kw=dict(projection='polar'))
def myValues(*args):
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 150, 10)
rho, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))
return theta, rho, values
# ---------------------------------Sub-plot #1 ---------------------------------
theta, rho, values = myValues()
im1 = axes.flat[0].contourf(theta, rho, values, 150, cmap='jet')
fig.colorbar(im1, ax=axes.flat[0]) # local colorbar
# ---------------------------------Sub-plot #2 ---------------------------------
theta, rho, values = myValues()
im2 = axes.flat[1].contourf(theta, rho, values, 150, cmap='jet')
# ---------------------------------Sub-plot #3 ---------------------------------
theta, rho, values = myValues()
im3 = axes.flat[2].contourf(theta, rho, values, 150, cmap='jet')
# ---------------------------------Sub-plot #4 ---------------------------------
theta, rho, values = myValues()
im4 = axes.flat[3].contourf(theta, rho, values, 150, cmap='jet')
# Global colorbar
fig.subplots_adjust(right=0.94)
cax = fig.add_axes([0.95, 0.15, 0.02, 0.7])
gcb = fig.colorbar(im4, cax=cax)
# This function is called periodically from FuncAnimation
def updateFig(*args):
global im1, im2, im3, im4, lcb, gcb
# ---------------------------------Sub-plot #1 ---------------------------------
theta, rho, values = myValues()
for c in im1.collections:
c.remove() # removes only the contours, leaves the rest intact
im1 = axes.flat[0].contourf(theta, rho, values, 150, cmap='jet')
# How to update this local colorbar?
lcb = fig.colorbar(im1, ax=axes.flat[0])
# ---------------------------------Sub-plot #2 ---------------------------------
theta, rho, values = myValues()
for c in im2.collections:
c.remove()
im2 = axes.flat[1].contourf(theta, rho, values, 150, cmap='jet')
# ---------------------------------Sub-plot #3 ---------------------------------
theta, rho, values = myValues()
for c in im3.collections:
c.remove()
im3 = axes.flat[2].contourf(theta, rho, values, 150, cmap='jet')
# ---------------------------------Sub-plot #4 ---------------------------------
theta, rho, values = myValues()
for c in im4.collections:
c.remove()
im4 = axes.flat[3].contourf(theta, rho, values, 150, cmap='jet')
# Update global colorbar
cax.cla()
gcb = fig.colorbar(im4, cax=cax)
# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, updateFig)
plt.show()
This post https://stackoverflow.com/questions/39472017/how-to-animate-the-colorbar-in-matplotlib seems to be relevant, but I was unable to adopt the suggested solution(s) to my case with subplots.