Last active
May 23, 2020 16:09
-
-
Save GeorgySk/63cffa9884044f8d6934332814d10c98 to your computer and use it in GitHub Desktop.
Matplotlib animation in Jupyter Lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Example of code for Matplotlib animation in Jupyter Lab""" | |
%matplotlib qt5 | |
from itertools import count | |
from functools import partial | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from matplotlib import animation | |
def plot(): | |
fig, ax = plt.subplots() | |
x = np.arange(0, 2 * np.pi, 0.01) | |
artists = dict(line=ax.plot(x, np.sin(x))[0]) | |
_ = animation.FuncAnimation(fig, | |
init_func=partial(_init, artists=artists), | |
func=_func, | |
frames=partial(_frames, x=x), | |
fargs=(artists,), | |
interval=2, | |
blit=True) | |
plt.show() | |
def _init(artists): | |
return list(artists.values()) | |
def _func(data, artists): | |
artists['line'].set_ydata(data) | |
return list(artists.values()) | |
def _frames(x): | |
for i in count(): | |
yield np.sin(x + i / 100) | |
plot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment