Animating A Map Of Ocean Temperature
I have 12 subplots showing changes in ocean temperature for the Celtic Sea. Each subplot is for a different month in the year. import xarray as xa import cmocean.cm as cm import ma
Solution 1:
Your case is excellent for the use of ArtistAnimation
, i.e., the flipbook approach using precomputed images. Sample code because the format of your animation is not specified:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, axes = plt.subplots(ncols=6, nrows=3, figsize=(15, 10))
#get specs for large image
gs = axes[0, -2].get_gridspec()
#remove unnecessary axis objectsfor ax in axes[0:, -2:].flat:
ax.remove()
#update axes list and label all static images
axes = fig.get_axes()
for i, ax inenumerate(axes):
ax.axis("off")
ax.set_title(f"month {i+1}")
#add axis object for large, animated image
ax_large = fig.add_subplot(gs[0:, -2:])
ax_large.axis("off")
#fake imagesdeff(x, y, i):
return np.sin(x*i/4) * i/6 + np.cos(y* (12-i)/4)
x = np.linspace(0, 2 * np.pi, 80)
y = np.linspace(0, 2 * np.pi, 120).reshape(-1, 1)
all_ims = []
min_v = -3
max_v = 3
ani_cmap = "seismic"for i, ax_small inenumerate(axes):
#image generation unnecessary for you because your images already exist
arr = f(x, y, i)
#static image into small frame
im_small = ax_small.imshow(arr, vmin=min_v, vmax=max_v, cmap=ani_cmap)
#animated image into large frame
im_large = ax_large.imshow(arr, animated=True, vmin=min_v, vmax=max_v, cmap=ani_cmap)
#animated images are collected in a list
all_ims.append([im_large])
ani = animation.ArtistAnimation(fig, all_ims, interval=200, blit=True)
plt.show()
Post a Comment for "Animating A Map Of Ocean Temperature"