Make Functions Interactive with Jupyter Widgets
Today I discovered a quick way to make functions in Jupyter Notebooks/Lab interactive. Using nothing but a function import and a decorator, I created a system for dynamically adjusting bins in a Matplotlib Pyplot histogram.
from ipywidgets import interact
#use interact decorator to [obviously] make this function interactive
@interact(bins=(1,200))
def widget_test(bins):
"""
Demonstrate the use of Jupyter widgets in adjusting Pyplot parameters.
"""
#import number generation and plotting libraries
import numpy as np
import matplotlib.pyplot as plt
#set title
plt.title("Dynamic Bucketing though Jovyan Widgetization")
#define random normal distribution with Numpy
distribution = np.random.normal(0,1,1000)
#plot histogram for distribution
plt.hist(distribution, bins=bins)