In [4]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
weights = np.load("./simulated_data/weights_10h.npy")
n_data = weights.shape[0]
N_out_x, N_out_y = 10, 10
N_out = N_out_x * N_out_y
# get a list of frames every 3 minutes
# frames = np.arange(0, n_data, 3)
# get a list of frames of the first hour
frames = np.arange(60)
# get a list of 150 frames exponentially speeding up
# frames = ((np.logspace(0, 1, 150) - 1) / 9 * (n_data - 1))
# frames = frames.astype(np.int)
seconds = 6
framelength = int(seconds * 1000 / frames.size)
weights_resh = weights.reshape(n_data, N_out, 20, 20)
fig, sps = plt.subplots(10, 10)
images = []
for i, sp in enumerate(sps.flat):
im = sp.imshow(weights_resh[-1, i, ...].squeeze(), interpolation="None", cmap="viridis")
images.append(im)
sp.set_xlabel("")
sp.set_ylabel("")
sp.set_xticklabels("")
sp.set_yticklabels("")
fig.set_size_inches(8, 8)
# plt.show()
In [5]:
# animation function. This is called sequentially
# get color minimum and maximum value from specific frame for all frames
cframe = 60
vmax = np.max(weights[cframe, ...])
vmin = np.min(weights[cframe, ...])
# vmax = np.max(weights)
# vmin = np.min(weights)
def animate(i):
# show progress in console
print(i, end=", ")
hours = np.floor((i + 1) / 60).astype(np.int)
minutes = ((i + 1) % 60).astype(np.int)
fig.suptitle("t = %1ih %.2imin" % (hours, minutes), fontsize=16)
for j, image in enumerate(images):
image.set_clim(vmax=vmax, vmin=vmin)
image.set_array(weights_resh[i, j, ...])
return images
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=framelength, blit=False)
HTML(anim.to_html5_video())
Out[5]:
In [3]:
anim.save('./visualisations/weight_change_jet.mp4', fps=15, extra_args=['-vcodec', 'libx264'])