Official document: https://matplotlib.org/api/animation_api.html
JSAnimation has been incorporated into matplotlib since matplotlib 2.1.
JSAnimation realizes animanation by continuously displaying images drawn by matplotlib.pyplot
.
Use the magic '% matplotlib notebook'. Note that it is not '% matplotlib inline'.
ArtistAnimation()
passes an array of generated images and displays them all together.
FuncAnimation()
displays every time an image is generated.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython import display
is_colab = 'google.colab' in str(get_ipython()) # for Google Colab
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots(1, 1, figsize=(6,4))
images=[]
for i in range(20):
y = np.sin(x + 2 * np.pi * (i/20))
img = ax.plot(x, y, color='b')
images.append(img)
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$y$")
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1.2, 1.2)
anim = animation.ArtistAnimation(fig, images, repeat=False, blit=True)
if is_colab: # for Google Colab
display.display(display.HTML(anim.to_jshtml()))
plt.close()
else:
plt.show()
anim.save('./anim13a.mp4')
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython import display
is_colab = 'google.colab' in str(get_ipython()) # for Google Colab
x = np.linspace(0, 2 * np.pi, 100)
x2 = np.arange(100)
fig, ax = plt.subplots(1, 2, figsize=(6*2,4))
images=[]
for i in range(20):
y = np.sin(x + 2 * np.pi * (i/20))
y2 = np.random.rand(len(x2))
img = ax[0].plot(x, y, color='b')
img2 = ax[1].plot(x2, y2, color='g')
images.append(img+img2)
ax[0].set_xlabel(r"$x$")
ax[0].set_ylabel(r"$y$")
ax[0].set_xlim(0, 2*np.pi)
ax[0].set_ylim(-1.2, 1.2)
ax[1].set_xlabel(r"$x$")
ax[1].set_ylabel(r"$y$")
ax[1].set_xlim(0, 100)
ax[1].set_ylim(0, 1)
anim = animation.ArtistAnimation(fig, images, repeat=False, blit=True)
if is_colab: # for Google Colab
display.display(display.HTML(anim.to_jshtml()))
plt.close()
else:
plt.show()
anim.save('./anim13b.mp4')
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython import display
is_colab = 'google.colab' in str(get_ipython()) # for Google Colab
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots(1, 1, figsize=(6,4))
def animate(i):
ax.clear()
y = np.sin(x + 2 * np.pi * (i/20))
img = ax.plot(x, y, color='b', label=r"$y=\sin x$")
ax.set_xlabel(r"$x$")
ax.set_ylabel(r"$y$")
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1.2, 1.2)
return img
anim = animation.FuncAnimation(fig, animate, frames=20, repeat=False, blit=True)
if is_colab: # for Google Colab
display.display(display.HTML(anim.to_jshtml()))
plt.close()
else:
plt.show()
anim.save('anim13c.mp4')
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython import display
is_colab = 'google.colab' in str(get_ipython()) # for Google Colab
x = np.linspace(0, 2 * np.pi, 100)
x2 = np.arange(100)
fig, ax = plt.subplots(1, 2, figsize=(6*2,4))
def animate(i):
ax[0].clear()
y = np.sin(x + 2 * np.pi * (i/20))
img = ax[0].plot(x, y, color='b')
ax[0].set_xlabel(r"$x$")
ax[0].set_ylabel(r"$y$")
ax[0].set_xlim(0, 2*np.pi)
ax[0].set_ylim(-1.2, 1.2)
ax[1].clear()
y2 = np.random.rand(len(x2))
img2 =ax[1].plot(x2, y2, color='g')
ax[1].set_xlabel(r"$x$")
ax[1].set_ylabel(r"$y$")
ax[1].set_xlim(0, 100)
ax[1].set_ylim(0, 1)
return img + img2
anim = animation.FuncAnimation(fig, animate, frames=20, repeat=False, blit=True)
if is_colab: # for Google Colab
display.display(display.HTML(anim.to_jshtml()))
plt.close()
else:
plt.show()
anim.save('anim13d.mp4')