Attaching play buttons to sliders¶
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…
Specifying Which Sliders Get Play buttons¶
%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()
bool
: All get a button¶
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)
str
: Position the button¶
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")
fig, ax = plt.subplots()
controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons="right")
list
: Choose by name¶
fig, ax = plt.subplots()
controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=["tau"])
defaultdict
: Specify by name and choose default¶
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,
)