Widgets from j1-nbinteract

The library j1-nbinteract comes with a set of functions that produce Javascript-based plots designed for interaction. The package combines the ipywidgets library and the bqplot plotting library to implement function-driven interfaces to interactive plotting.

The nbinteract plotting methods use ipywidgets to generate and display widgets, inferring the widget type as needed. When a user interacts with a widget, a Python callback updates the visualization without a complete rerender. This noticeably lowers visualization update time compared to using ipywidgets alone to render static images.

import nbinteract as nbi
import numpy as np
from numpy import arange

Histogram

The widget nbinteract.nbinteract.hist generates a histogram that allows interaction with the parameters for the response function. The widget takes in a single response function. The response function returns the array of numerical values that will be shown in the histogram. The hist function allows interaction with the response function's parameters by specifying them as keyword arguments in the same format as ipywidgets.interact. Any argument that can be used for ipywidgets.interact can be used for hist.

def hist_function(mean, sd, size=1000):
    '''
    Returns 1000 values picked at random from the normal
    distribution with the Mean value and SD given.
    '''
    return np.random.normal(loc=mean, scale=sd, size=1000)


options = {
    'title': 'Histogram',
    'xlabel': 'Mean value (mean)',
    'ylabel': 'Standard Deviation (sd)',
    'bins': 10
}

layouts = {
    'plot_height': '480px',
    'plot_width': '800px',
}

hist_chart = nbi.hist(
    hist_function,
    mean=(0, 10),
    sd=(0.2, 2.0, 0.2),
    options=options,
    layouts=layouts
)

hist_chart

Barchart

The widget nbinteract.nbinteract.bar generates an bar plot that allows interaction with the parameters for the response functions.

The first two arguments of bar are response functions that return the x and y-axis data arrays, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to bar in the format expected by interact. The response function for the y-axis data will be called with the x-axis data as its first argument.

For example, in the bar plot below categories generates the categories to plot on the x-axis and heights generates the y-axis heights. The heights function uses the parameter xs which is the array of x-axis data points.

def x_fn(n): 
    return np.arange(n)

def y_fn(xs, offset):
    return xs + offset


options = {
    'title': 'Barchart',
    'xlabel': 'Number of bars (n)',
    'ylabel': 'Offset  value',
    'animation_duration': 500,
    'aspect_ratio': 1.778,
    'ylim': (0, 20)
}

layouts = {
    'plot_height': '480px',
    'plot_width': '800px',
}

bar_chart = nbi.bar(
    x_fn,
    y_fn,
    n=(3, 10),
    offset=(1, 10),
    options=options,
    layouts=layouts
)

bar_chart

Interactive Scatter chart

The widget nbinteract.scatter_drag generates a scatter plot that allows interaction through clicking and dragging the points on the graph.

scatter_drag takes in two lists/arrays consisting of the x-coordinates and y-coordinates of the points to plot. It generates an interactive scatter plot where the points can be dragged by the user and a best fit line is updated automatically according to the placement of the points.

scatter_drag does not allow response functions as inputs.

x_points = np.arange(10)
y_points = np.arange(10) + np.random.rand(10)


options = {
    'title': 'Interactive Scatter Plot',
    'animation_duration': 1000,
    'xlabel': 'x-values',
    'ylabel': 'y-values',
    'xlim': (0, 10),
    'ylim': (0, 10)
}

layouts = {
    'plot_height': '480px',
    'plot_width': '800px',
}

show_eqn = False

scatter_drag = nbi.scatter_drag(
    x_points,
    y_points,
    options=options,
    layouts=layouts
)

scatter_drag

Scatter chart

The scatter widget nbinteract.scatter generates a scatter plot that allows interaction with the parameters to the response functions. This is different from scatter_drag which facilitates interaction using click and drag actions.

The first two arguments of scatter are response functions that return the x and y-axis coordinates, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to scatter in the format expected by interact. The response function for the y-coordinates will be called with the x-coordinates as its first argument.

def x_fn(n):
    return np.random.choice(100, n)
    
def y_fn(xs):
    return np.random.choice(100, len(xs))


options = {
    'title': 'Scatter Plot',
    'marker': 'circle',
    'animation_duration': 1000,    
    'xlabel': 'x-values',
    'ylabel': 'y-values',
    'xlim': (0, 100),
    'ylim': (0, 100)
}

layouts = {
    'plot_height': '480px',
    'plot_width': '800px',
}

scatter_chart = nbi.scatter(
    x_fn,
    y_fn,
    n=(10,200),
    options=options,
    layouts=layouts
)

scatter_chart

Line chart

The line widget nbinteract.line generates a line plot that allows interaction with the parameters to the response functions.

The first two arguments of line are response functions that return the x and y-axis coordinates, respectively. Either argument can be arrays themselves. Arguments for the response functions must be passed in as keyword arguments to line in the format expected by interact. The response function for the y-coordinates will be called with the x-coordinates as its first argument.

def x_fn(max):
    return np.arange(0, max + 1)

def y_fn(xs, sd):
    return xs + np.random.normal(0, scale=sd, size=len(xs))


options = {
    'title': 'Line chart',
    'xlabel': 'x-values (max)',
    'ylabel': 'y-values (sd)',    
    'xlim': (0, 50),
    'ylim': (-20, 70),
    'animation_duration': 500
}

layouts = {
    'plot_height': '480px',
    'plot_width': '800px',
}

line_chart = nbi.line(
    x_fn,
    y_fn,
    max=(10, 50),
    sd=(0, 10),
    options=options,
    layouts=layouts
)

line_chart

Interactive Questions

nbinteract also provides built-in methods to easily create multiple choice and short answer questions.

Multiple choice

The widget nbinteract.multiple_choice takes a question, a list of possible answer choices, and the correct answer. Clicking the buttons shows whether the choice was correct.

nbi.multiple_choice(question="What is 10 + 2 * 5?",
                    choices=['12', '60', '20'],
                    answers=2)
nbi.multiple_choice(question="Select all prime numbers.",
                    choices=['12', '3', '31'],
                    answers=[1, 2])

Short answer

The widget nbinteract.short_answer takes a question and an answer. The answer can either be a string, a list of strings, or a function that returns True when called with the user's input. If the function errors, an error message will be displayed.

nbi.short_answer('What is 1+1?', answers='2', explanation='1+1 is 2')
nbi.short_answer('Enter the first name of a member of the Beatles.',
                 ['John', 'Paul', 'George', 'Ringo'])
nbi.short_answer('Enter an even number.', lambda x: int(x) % 2 == 0)