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!