Question on filtering data with widget and showing on plot

I’m trying to get the value from a slider and filter data based on that value and show the filtered data.

The way I’m doing it below is
-same data in both origin and source datasets
-plot first with origin
-filter data in source with a new value and update origin data
-continue

However, I can’t seem to be able to get it to work.
Please help…

x = np.linspace(0,10,10)
y = np.linspace(20,30,10)
z = [0,0,1,0,1,0,1,0,1,1]

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

p = figure(plot_width=600, plot_height=400)
p.circle(‘x’, ‘y’, source=source, size=5, color=“navy”,alpha=0.5)

def callback(source=source, source2 = origin, origin = origin, window=None):
data = source2.data
r_data = source.data
data_dct_l = zip(r_data[‘x’], r_data[‘y’], r_data[‘z’])
f = cb_obj.value

filtered = [x for x in data_dct_l if x[2] == f]
for n, colname in enumerate('x y z'.split()):
    r_data[colname] = [i[n] for i in filtered]
   
r_data.trigger('change')    

sample_slider = Slider(start=0, end=1, value= 0, step=1,
title=“n”, callback=CustomJS.from_py_func(callback))

layout = column(sample_slider, p)
show(layout)

``

I'm not actually sure from_py_func is smart enough to handle things like list comprehensions, zip, and enumerate. You'd have to look at the Flexx docs to be sure (the function is implemented using the Flexx package) or directly inspect the JS that gets created and put in your document to see if it is reasonable.

Thanks,

Bryan

···

On Jan 16, 2017, at 8:03 PM, [email protected] wrote:

I'm trying to get the value from a slider and filter data based on that value and show the filtered data.

The way I'm doing it below is
-same data in both origin and source datasets
-plot first with origin
-filter data in source with a new value and update origin data
-continue

However, I can't seem to be able to get it to work.
Please help...

x = np.linspace(0,10,10)
y = np.linspace(20,30,10)
z = [0,0,1,0,1,0,1,0,1,1]

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

p = figure(plot_width=600, plot_height=400)
p.circle('x', 'y', source=source, size=5, color="navy",alpha=0.5)

def callback(source=source, source2 = origin, origin = origin, window=None):
    data = source2.data
    r_data = source.data
    data_dct_l = zip(r_data['x'], r_data['y'], r_data['z'])
    f = cb_obj.value

    filtered = [x for x in data_dct_l if x[2] == f]
    for n, colname in enumerate('x y z'.split()):
        r_data[colname] = [i[n] for i in filtered]
        
    r_data.trigger('change')
        
sample_slider = Slider(start=0, end=1, value= 0, step=1,
                       title="n", callback=CustomJS.from_py_func(callback))

layout = column(sample_slider, p)
show(layout)

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/11d2c7f7-f0fe-44dd-8334-3a506ff7d801%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.