Skip to content Skip to sidebar Skip to footer

Bokeh Widget Slicing Data

I am trying to create a plot using bokeh to visualize my data on IPython Notebook. I want to add some widgets to make it more interactive. Below is an example of the codes. from bo

Solution 1:

The issue is you are not actually changing the contents of data['x'], data['y'] or data['z'].

Second issue is, if you change the source data, then you cant change it back as it no longer contains the full data you begun with. Work around is passing in the original data to the callback, and assigning the filtered data to the ColumnDataSource, without changing the original data.

from bokeh.models import CustomJS, ColumnDataSource
from bokeh.plotting import Figure, output_notebook, show
from bokeh.models.widgets import Select
from bokeh.layouts import column

output_notebook()

x = [x*0.005for x inrange(0, 200)]
y = x
z = ['A'if i>50else'B'for i inrange(len(x))]

original_source = ColumnDataSource(data=dict(x=x, y=y, z=z))
source = ColumnDataSource(data=dict(x=x, y=y, z=z))

plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

defcallback(source=source, original_source = original_source):
    data=original_source.data
    s_data = source.data
    f = cb_obj.value
    x, y, z = data['x'], data['y'], data['z']
    x = [x[i] for i inrange(len(x)) if z[i] == f]
    y = [y[i] for i inrange(len(y)) if z[i] == f]
    z = [z[i] for i inrange(len(z)) if z[i] == f]
    s_data['x'] = x
    s_data['y'] = y
    s_data['z'] = z
    source.trigger('change')

slides = Select(title="Option:", value = 'A', options=['A', 'B'], 
                callback=CustomJS.from_py_func(callback))

layout = column(slides,plot)

show(layout)

Post a Comment for "Bokeh Widget Slicing Data"