Custom Callbacks and Accessing Parameter Values¶
Custom Callbacks¶
While mpl_interactions
provides some functions for styling plots it does not cover everything. To allow for a full suite of plot customization or connections to other parts of your code you can register custom callback functions with the controls
object.
[ ]:
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
import mpl_interactions.ipyplot as iplt
[ ]:
# define the function
def f(x, tau, beta):
return np.sin(x * tau) * beta
x = np.linspace(0, 2 * np.pi, 1000)
tau = np.linspace(5, 10)
beta = np.linspace(0.25, 1)
[ ]:
# make the interactive figure
fig, ax = plt.subplots()
controls = iplt.plot(x, f, tau=tau, beta=beta)
# attach a custom callback
# if running from a script you can just delete the widgets.Output and associated code
import ipywidgets as widgets
out = widgets.Output()
display(out)
def my_callback(beta):
with out:
print(f"beta is: {beta}!!!")
controls.register_callback(my_callback, "beta")
Calling the callback on registration¶
Sometimes you may want the callback to be called as soon as you register it. To accomplish this use the eager
argument. This can be useful when your callback styles the plot based on the current parameters and you want the initial plot to be subject to this styling.
[ ]:
fig, ax = plt.subplots()
controls = iplt.plot(x, f, tau=tau, beta=beta)
# attach a custom callback
# if running from a script you can just delete the widgets.Output and associated code
out = widgets.Output()
display(out)
def my_callback(beta):
with out:
print(f"beta is: {beta}!!!")
controls.register_callback(my_callback, "beta", eager=True)
Accepting multiple parameters¶
To accept multiple parameters use a list
for the params
argument to register_callback
.
[ ]:
fig, ax = plt.subplots()
controls = iplt.plot(x, f, tau=tau, beta=beta)
# attach a custom callback
# if running from a script you can just delete the widgets.Output and associated code
out2 = widgets.Output()
display(out2)
def my_callback(tau, beta):
with out2:
print(f"beta is: {beta}!!!")
print(f"tau is: {tau}")
print(":O")
controls.register_callback(my_callback, ["tau", "beta"])
A more complex callback¶
You can put arbitrary python code into these callbacks. This means that you can do complex things like styling the plot in arbitrary ways that would not be easy to achieve with the functions provided by mpl-interactions
. Here we will change the color of the x_ticks
and y_ticks
conditional on the values of tau
and beta
[ ]:
fig, ax = plt.subplots()
controls = iplt.plot(x, f, tau=tau, beta=beta)
# attach a custom callback
# if running from a script you can just delete the widgets.Output and associated code
def my_callback(tau, beta):
if tau < 7.5:
ax.tick_params(axis="x", colors="red")
else:
ax.tick_params(axis="x", colors="black")
if beta < 0.5:
ax.tick_params(axis="y", colors="black")
else:
ax.tick_params(axis="y", colors="blue")
controls.register_callback(my_callback, ["tau", "beta"], eager=True)
Accessing parameters¶
After manually tuning the sliders you may want to access the values you have chosen permanently for usage in other parts of you code. The current parameter values are stored in a dictionary in the controls object that can be accessed by controls.params
[ ]: