Using Bokeh for interactive plotting in an offline envirornment

Imports

In [ ]:
from bokeh.embed import file_html
from bokeh.layouts import layout
from bokeh.models import CustomJS, ColumnDataSource, Slider, HoverTool, TapTool,OpenURL
from bokeh.io import output_notebook, show, output_file
from bokeh.plotting import figure
from bokeh.resources import INLINE
from bokeh.layouts import widgetbox, column
import numpy as np
import names

Data preporation

In [ ]:
N=1000

X = np.random.random(N)*100
Y = np.random.random(N)*100
full_name_list = [names.get_full_name() for x in X]
first_name_list = [name.split()[0] for name in full_name_list]
last_name_list = [name.split()[1] for name in full_name_list]

rad = np.random.random(N)*1.5
colors = ['#%02x%02x%02x' % (r,g,150) for r, g in zip([int(t) for t in np.floor(50+2*X)], [int(t) for t in np.floor(30+2*Y)])]

source = ColumnDataSource(data=dict(
    color=colors, 
    rad=rad,
    rad_o=rad,
    x=X,
    y=Y,
    full_name=full_name_list,
    first_name=first_name_list,
    last_name = last_name_list
))

User defined functions

In [10]:
def save_or_show_plot(layout, filename='', title=''):

    if filename:
        output_file(filename, title=title, mode='inline')
        show(layout)
    else:
        output_notebook(INLINE)
        show(layout)

Figure 1 - Interactive RBG of points

In [12]:
plot = figure(width=900, plot_height=500)
plot.circle('x', 'y', radius='rad', fill_color='color', source=source, fill_alpha=0.6, line_color=None)

r_slider = Slider(start=0, end=255, value=0, step=1, title="R")
g_slider = Slider(start=0, end=255, value=0, step=1, title="G")
b_slider = Slider(start=0, end=255, value=0, step=1, title="B")

update_points_color = CustomJS(
    args=dict(
        source=source, 
        r_slider=r_slider, 
        g_slider=g_slider,
        b_slider=b_slider
    ), code="""
    var data = source.data;
    var r = r_slider.value;
    var g = g_slider.value;
    var b = b_slider.value;
    color = data.color;
    
    for (i = 0; i < color.length; i++) {
        data.color[i] = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
    }
    
    source.change.emit();
""")
r_slider.js_on_change('value', update_points_color)
g_slider.js_on_change('value', update_points_color)
b_slider.js_on_change('value', update_points_color)

save_or_show_plot(layout([[r_slider, g_slider, b_slider], [plot]]), 'bokeh.html', 'JS Events Example')
Loading BokehJS ...