.. note:: The labels will not update as that requires a Python kernel.
If you are working in Jupyter then you can add
an ipywidgets.Play widget to the sliders for any of the interactive_*
functions.
In this tutorial all the functions are scatter
but this will work for plot
, hist
, imshow
, etc...
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
import mpl_interactions.ipyplot as iplt
# turn off interactive mode so that broken
# plots don't render in the docs
plt.ioff()
N = 50
x = np.random.rand(N)
def f_y(x, tau, beta):
return np.sin(x * tau) ** 2 + np.random.randn(N) * 0.01 * beta
fig, ax = plt.subplots()
controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=True)
VBox(children=(HBox(children=(Play(value=0, max=99), IntSlider(value=0, description='tau', max=99, readout=Fal…
You make a play button for all sliders on either the left or the right using a string argument.
fig, ax = plt.subplots()
controls = iplt.scatter(
x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons="left"
)
VBox(children=(HBox(children=(Play(value=0, max=99), IntSlider(value=0, description='tau', max=99, readout=Fal…
fig, ax = plt.subplots()
controls = iplt.scatter(
x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons="right"
)
VBox(children=(HBox(children=(IntSlider(value=0, description='tau', max=99, readout=False), Label(value='1.00'…
fig, ax = plt.subplots()
controls = iplt.scatter(
x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=["tau"]
)
VBox(children=(HBox(children=(Play(value=0, max=99), IntSlider(value=0, description='tau', max=99, readout=Fal…
If you have many parameters and you want the most, but not all, of them to have a Play button then you should use a `defaultdict``
from collections import defaultdict
def f(x, **kwargs):
return x
play_buttons = defaultdict(lambda: True)
play_buttons["tau"] = False
fig, ax = plt.subplots()
controls = iplt.scatter(
x,
f,
tau=(1, 2 * np.pi, 100),
beta=(0, 2),
zeta=(0, 1),
psi=(0, 1),
play_buttons=play_buttons,
)
VBox(children=(HBox(children=(IntSlider(value=0, description='tau', max=99, readout=False), Label(value='1.00'…