I read a lot of answers here and tutorials, but I cannot get this code to work. I have a figure with three subplots on one row. Each subplot shows a matrix. I wrote a for loop to update the visualization of the matrices with a different numpy array at every iteration. The figure opens, shows an initial state, does not update and closes.
import matplotlib.pyplot as plt
from matplotlib import cm
import time
import numpy as np
cmap_veg_wat = cm.get_cmap("Greens").copy()
cmap_veg_wat.set_under('#409fff')
plt.ion()
fig, ax = plt.subplots(1, 3, num=None, figsize=(10,10), dpi=80, facecolor='w', edgecolor='k')
index = count()
ind = 0
MATR1 = np.load('erb.npy')
MATR2 = np.load('carv.npy')
MATR3 = np.load('vegwat.npy')
ax[0].imshow(MATR2, cmap='Reds')
ax[1].imshow(MATR1, cmap='Greens')
ax[2].imshow(MATR3, cmap=cmap_veg_wat, vmin=0)
for x in range(3):
npErb = 'erb'+ str(x) + '.npy'
npCarv = 'carv'+ str(x) + '.npy'
npVW = 'vegwat'+ str(x) + '.npy'
MATR1 = np.load(npErb)
MAMTR2 = np.load(npCarv)
MATR3 = np.load(npVW)
x_time = np.load('xT.npy')
y_erbpopulation = np.load('yE.npy')
y_carvpopulation = np.load('yC.npy')
y_vegdensity = np.load('yV.npy')
ax[0].imshow(MATR2, cmap='Reds')
ax[1].imshow(MATR1, cmap='Greens')
ax[2].imshow(MATR3, cmap=cmap_veg__wat, vmin=0)
fig.canvas.draw()
fig.canvas.flush_events()
time.sleep(0.1)
I think that, mutatis mutandis, this is what you want.
I'm sorry I cannot post the animation, but if you save my code to a script, e.g. 3matrices.py
and next execute it, $ python3 3matrices.py
, then you'll see the animation unfold in front of you...
import matplotlib.pyplot as plt
import numpy as np
import time
cmaps = 'Reds Greens Blues'.split()
norm = plt.Normalize(vmin=0, vmax=240)
plt.ion()
fig, axs = plt.subplots(1, 3, figsize=(10,10), sharey='all',
dpi=80, edgecolor='k', linewidth=3)
for x in range(4):
fig.suptitle(str(x+1))
mx = np.arange(60).reshape(15, 4)*(x+1)
for ax, cmap in zip(axs, cmaps):
ax.imshow(mx, cmap=cmap, norm=norm)
fig.canvas.draw()
fig.canvas.flush_events()
if x<3 : time.sleep(1.0)
input('Press Invio to exit')