Retrieving DataFrame based on X-Range of Plot

Hello, community.

So I’ve been trying to do the following: when zooming in on the plot, I want to retreive the data frame only for that displayed data.

My problem is that I don’t understand how to fetch the data from the currently displayed x-range with the p.on-change and feed it in the update_data function.

For the update function I can retrieve the start and end points of the x-range.

Here my code:

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.models.widgets import CheckboxButtonGroup, DataTable, TableColumn

from bokeh.layouts import widgetbox,column

from bokeh.sampledata.autompg import autompg as df

CDS = ColumnDataSource(df)

columns = [

TableColumn(field = ‘mpg’, title = ‘mpg’),

#TableColumn(field = ‘cyl’, title = ‘cyl’),

#TableColumn(field = ‘displ’, title = ‘displ’),

TableColumn(field = ‘hp’, title = ‘hp’),

#TableColumn(field = ‘weight’, title = ‘weight’),

#TableColumn(field = ‘accel’, title = ‘accel’),

#TableColumn(field = ‘yr’, title = ‘yr’),

#TableColumn(field = ‘origin’, title = ‘origin’),

#TableColumn(field = ‘name’, title = ‘name’),

]

set up figure

p = figure(plot_width = 400, plot_height = 400)

add a line renderer

line= p.line(x = ‘index’, y = ‘mpg’, source = CDS, line_width=2)

add table with data content

data_table = DataTable(source = CDS, columns = columns, width = 800, height = 600)

Update y to read different co df[(df) & ()]lumn in CDS

def update(attrname, old, new):

#print(line.glyph.y)

#print(p.x_range.start)

#print(p.x_range.end)

if line.glyph.y == ‘mpg’:

line.glyph.y = ‘hp’

else:

line.glyph.y = ‘mpg’

#print(line.glyph.y)

#print(round(p.x_range.start, 0))

#print(round(p.x_range.end,0))

#print(df.iloc[int(round(p.x_range.start, 0)):int(round(p.x_range.end,0)),:])

return

def update_data(attrname, old, new):

x_start = int(round(p.x_range.start, 0))

x_end = int(round(p.x_range.end, 0))

···

print(df.iloc[x_start:x_end,:])

return

checkbox = CheckboxButtonGroup(labels=[“Change Column”], active=, button_type=“primary”)

checkbox.on_change(‘active’, update)

p.on_change(‘x_range’, update_data)

inputs = widgetbox(checkbox, width=300)

curdoc().add_root(column(inputs, p, data_table, width=1100))

I would also appreciate to know where I can find any documentation cause on the official website I was unable to find any answer. How do I know p.x_range.start is the correct way to reference to the start of the x-range? Where is this information documented?

Thanks for your support!

Ok, so I’ve been able to retrieve the x_range.start and x_range.end by doing the following:

def update_data_start(attrname, old, new):

val = int(round(new,0))

···

print('Start ', val)

return val

def update_data_end(attrname, old, new):

val = int(round(new,0))

print('End ', val)

return val

p.x_range.on_change(‘start’, update_data_start)

p.x_range.on_change(‘end’, update_data_end)

But how can I use the values from the functions update_data_start respectivel update_data_end to adapt my dataframe as desired?
I’ve tried following but without any success:

def trim_data (attrname, old, new):

start = update_data_start(attrname, old, new)

end = update_data_end(attrname, old, new)

print (df.iloc[int(round(start, 0)):int(round(end, 0))])

return

On Wednesday, March 20, 2019 at 3:21:45 PM UTC+1, golanu’ de la mare wrote:

Hello, community.

So I’ve been trying to do the following: when zooming in on the plot, I want to retreive the data frame only for that displayed data.

My problem is that I don’t understand how to fetch the data from the currently displayed x-range with the p.on-change and feed it in the update_data function.

For the update function I can retrieve the start and end points of the x-range.

Here my code:

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.models.widgets import CheckboxButtonGroup, DataTable, TableColumn

from bokeh.layouts import widgetbox,column

from bokeh.sampledata.autompg import autompg as df

CDS = ColumnDataSource(df)

columns = [

TableColumn(field = ‘mpg’, title = ‘mpg’),

tablecolumn(field = ‘cyl’, title = ‘cyl’),

tablecolumn(field = ‘displ’, title = ‘displ’),

TableColumn(field = ‘hp’, title = ‘hp’),

tablecolumn(field = ‘weight’, title = ‘weight’),

tablecolumn(field = ‘accel’, title = ‘accel’),

tablecolumn(field = ‘yr’, title = ‘yr’),

tablecolumn(field = ‘origin’, title = ‘origin’),

tablecolumn(field = ‘name’, title = ‘name’),

]

set up figure

p = figure(plot_width = 400, plot_height = 400)

add a line renderer

line= p.line(x = ‘index’, y = ‘mpg’, source = CDS, line_width=2)

add table with data content

data_table = DataTable(source = CDS, columns = columns, width = 800, height = 600)

Update y to read different co df[(df) & ()]lumn in CDS

def update(attrname, old, new):

#print(line.glyph.y)

#print(p.x_range.start)

#print(p.x_range.end)

if line.glyph.y == ‘mpg’:

line.glyph.y = ‘hp’

else:

line.glyph.y = ‘mpg’

#print(line.glyph.y)

#print(round(p.x_range.start, 0))

#print(round(p.x_range.end,0))

#print(df.iloc[int(round(p.x_range.start, 0)):int(round(p.x_range.end,0)),:])

return

def update_data(attrname, old, new):

x_start = int(round(p.x_range.start, 0))

x_end = int(round(p.x_range.end, 0))

print(df.iloc[x_start:x_end,:])

return

checkbox = CheckboxButtonGroup(labels=[“Change Column”], active=, button_type=“primary”)

checkbox.on_change(‘active’, update)

p.on_change(‘x_range’, update_data)

inputs = widgetbox(checkbox, width=300)

curdoc().add_root(column(inputs, p, data_table, width=1100))

I would also appreciate to know where I can find any documentation cause on the official website I was unable to find any answer. How do I know p.x_range.start is the correct way to reference to the start of the x-range? Where is this information documented?

Thanks for your support!