I am plotting in a figure in a loop. The data range changes every time. I would like to update my colorbar based on new range but instead of updating the current colorbar another colorbar will be added to my figure. I tried to use fig.add_axes to set a constant axes for my colorbar but still I can see that the colorbars are added in that particular axes on top of each other which doesn't look nice:
from matplotlib import pyplot as plt
fig1 = plt.figure()
fig1.clf()
ax = fig1.add_subplot(111)
temp = numpy.mymatrix
im1 = ax.imshow((temp), cmap='coolwarm', aspect='equal')
cbaxes = fig1.add_axes([0.8, 0.1, 0.03, 0.8])
plt.colorbar(im1, cax = cbaxes)
plt.pause(0.00000001)
is there a solution in which I can update the existing colorbar?
You could call update_normal()
on the colorbar.
Here is an example in a loop:
from matplotlib import pyplot as plt
import numpy as np
fig1 = plt.figure()
ax = fig1.add_subplot(111)
cbaxes = fig1.add_axes([0.8, 0.1, 0.03, 0.8])
for i in range(100):
temp = np.random.normal(0, 1, size=(10,10))
im1 = ax.imshow(temp, cmap='coolwarm', aspect='equal')
if i == 0:
cbar = plt.colorbar(im1, cax = cbaxes)
else:
cbar.update_normal(im1)
plt.pause(0.00000001)