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) 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

In [ ]:
%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()

Boolean: All get a button

In [ ]:
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)

String:

You make a play button for all sliders on either the left or the right using a string argument.

In [ ]:
fig, ax = plt.subplots()
controls = iplt.scatter(
    x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons="left"
)
In [ ]:
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

In [ ]:
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``

In [ ]:
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,
)
In [ ]: