Plotting using nbinteract

nbinteract comes with a set of functions that produce Javascript-based plots designed for interaction.

Most plotting functions that come with nbinteract take in response functions that return the data to be plotted.


In [1]:
import nbinteract as nbi
import numpy as np

For a complete API reference for each function, you may type the function name in a cell and add a ? at the end. For example, to view the API reference for nbi.hist:


In [2]:
nbi.hist?

nbinteract.hist

hist generates a histogram that allows interaction with the parameters for the response function.

hist 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.


In [3]:
def hist_response_function(mean, sd, size=1000):
    '''
    Returns 1000 values picked at random from the normal
    distribution with the mean and SD given.
    '''
    return np.random.normal(loc=mean, scale=sd, size=1000)

In [4]:
nbi.hist(hist_response_function, mean=(0, 10), sd=(0, 2.0, 0.2))


If you interact with the above plot, you may notice that the plot's x and y-axes will automatically scale to match the input data. You can change plot parameters like the axes limits through the options parameter of the plotting functions:


In [5]:
options = {
    'title': '1000 random points from normal distribution',
    'xlim': (0, 15),
    'ylim': (0, 0.4),
}
nbi.hist(hist_response_function, options=options, mean=(0, 10), sd=(0, 2.0, 0.2))


You may call nbinteract plotting functions with plain data as the input as well:


In [6]:
nbi.hist(np.random.normal(size=1000))


nbinteract.bar

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.


In [7]:
def categories(n): 
    return np.arange(n)

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

opts = {
    'ylim': (0, 20),
}

nbi.bar(categories, heights, n=(0, 10), offset=(1, 10), options=opts)


nbinteract.scatter_drag

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.


In [8]:
x_coords = np.arange(10)
y_coords = np.arange(10) + np.random.rand(10)

opts = {'xlim': (0, 9), 'ylim': (0, 11), 'animation_duration': 250}

nbi.scatter_drag(x_coords, y_coords, options=opts)


nbinteract.scatter

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.


In [9]:
def x_values(n): return np.random.choice(100, n)
def y_values(xs): return np.random.choice(100, len(xs))

nbi.scatter(x_values, y_values, n=(0,200))


nbinteract.line

line generates a scatter 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.


In [10]:
def x_values(max): return np.arange(0, max)
def y_values(xs, sd):
    return xs + np.random.normal(0, scale=sd, size=len(xs))

opts = {
    'xlim': (0, 50),
    'ylim': (0, 55),
    'animation_duration': 250,
}

nbi.line(x_values, y_values, max=(10, 50), sd=(1, 10), options=opts)