☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

How to change plot design using callback in bokeh

Community Supportbokehjs
Abhi
Abhi

I want to create dynamic plot in bokeh. Once user select any number of variables from multi select option then it will create that many different line graph in that many subplots. I tried below python code to achieve that

from bokeh.io import output_file, show,curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.models import HoverTool
import pandas as pd
import numpy as np
#Read in the data

data = {'Date': pd.date_range('1999-01-01','2000-07-16',freq = 'D'),'v1':np.random.randint(1,50,size = 563),
        'v2':np.random.randint(40,90,size = 563),'v3':np.random.randint(110,250,size = 563),
        'v4':np.random.randint(300,500,size = 563)}
df = pd.DataFrame(data)


#Create the ColumnDataSource object

#df_filter = df[['Date','v1','v2','v3 ']]
#Create the hover tooltip

hover_tool = HoverTool(tooltips = [
    ('Date', '@x{%Y-%m-%d %H:%M:%S.%3Ns}'),
    ("(x,y)", "($x, $y)"),
    ('value', '@y')
],formatters={'@x': 'datetime'})

plot = figure()

from bokeh.models import MultiSelect

from bokeh.models.widgets import DateRangeSlider

from datetime import date


#Create the menu

options = [(col,col) for col in df.columns if col != 'Date']

#Create the Dropdown

select = MultiSelect(title="Select the variable:", value=["v1"], options=options)

dt_range_slider = DateRangeSlider(start=date(1999,1,1), end=date(2000,7,16), value=(date(1999,1,1),date(2000,7,16)), step=31*24*60*60*1000, title="Date Range:")


def callback(attr, old, new):    
    var = select.value
    a,b = dt_range_slider.value[0],dt_range_slider.value[1]
    a  = pd.to_datetime(a,unit = 'ms')
    b  = pd.to_datetime(b,unit = 'ms')
    df1 = df[(df['Date'] >= a) & (df['Date'] <= b)]
    plots = []
    for i in range(len(var)):
        p = figure(title = 'This is a testing graph',x_axis_type = 'datetime', x_axis_label = 'date', y_axis_label = 'High Prices',tools = [hover_tool,'box_select','pan','wheel_zoom','box_zoom','save','reset','help'])
        glyphs = [p.line(df1['Date'], df1[var[i]]) for j in range(len(var)-1)]
        plots.append(p)
    layout.children[1].children[0].children = plots



select.on_change('value', callback)

dt_range_slider.on_change('value',callback)

from bokeh.layouts import row,column

layout = row(column(select,dt_range_slider),plot)

curdoc().add_root(layout)

But unable to yield any result. Please help me on this

⌁   Link to post

End of archived discussion · Replies are disabled