I have tried working with interactive graphs and everything works really well, however bokeh module dosent have interactive histograms, and thus i used a function available online to make histograms and now i am trying to update the histogram plots with the sliders and it dosent seem to work well
below is the code, if someone could please help me make it work??
import pandas as pd
from pandas import DataFrame
import numpy as np
from bokeh.models import HoverTool , CategoricalColorMapper
from bokeh.themes import built_in_themes
from bokeh.io import curdoc
from bokeh.models import Slider,DataTable, TableColumn
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.layouts import row, column, gridplot
from bokeh.palettes import Category10_5, Category20_16
from bokeh.models.widgets import Tabs, Panel
from bokeh.core.properties import Float, Instance, Tuple, Bool, Enum
from bokeh.models import InputWidget
from bokeh.models.callbacks import Callback
from bokeh.models.widgets import RangeSlider
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
source = ColumnDataSource(df)
A_Slider= RangeSlider(start=min(df['A']), end=max(df['A']), value=(min(df['A']),max(df['A'])), step=1, title='YY in mm')
B_Slider = RangeSlider(start=min(df['B']), end=max(df['B']), value=(min(df['B']),max(df['B'])), step=1, title='ZZ')
dff=df
def callback(attr,new,old):
A_s = A_Slider.value[0]
A_e = A_Slider.value[1]
B_s= B_Slider.value[0]
B_e= B_Slider.value[1]
dff= pd.DataFrame(df[(df.A >=A_s) & (df.A <= A_e) & (df.B >= B_s) & (df.B <= B_e)])
source.data = ColumnDataSource.from_df(dff)
#function ends here
A_Slider.on_change("value",callback)
B_Slider.on_change("value",callback)
# Histogram
def interactive_histogram(dff,col,n_bins,bin_range,title,x_axis_label,x_tooltip):
arr_hist, edges = np.histogram(dff[col],bins=n_bins,range=bin_range)
# Column data source
arr_df = pd.DataFrame({'count': arr_hist, 'left': edges[:-1], 'right': edges[1:]})
arr_df['f_count'] = ['%d' % count for count in arr_df['count']]
arr_df['f_interval'] = ['%d to %d ' % (left, right) for left, right in zip(arr_df['left'], arr_df['right'])]
source = ColumnDataSource(arr_df)
# Set up the figure same as before
toollist = ['lasso_select', 'tap', 'reset', 'save','crosshair','wheel_zoom','pan','hover','box_select']
p = figure(plot_width = 500,
plot_height = 500,
title = title,
x_axis_label = x_axis_label,
y_axis_label = 'Count',tools=toollist)
# Add a quad glyph with source this time
p.quad(bottom=0,
top='count',
left='left',
right='right',
source=source,
fill_color='red',
hover_fill_alpha=0.7,
hover_fill_color='blue',
line_color='black')
# Add style to the plot
p.title.align = 'center'
p.title.text_font_size = '18pt'
p.xaxis.axis_label_text_font_size = '12pt'
p.xaxis.major_label_text_font_size = '12pt'
p.yaxis.axis_label_text_font_size = '12pt'
p.yaxis.major_label_text_font_size = '12pt'
# Add a hover tool referring to the formatted columns
hover = HoverTool(tooltips = [(x_tooltip, '@f_interval'),
('Count', '@f_count')])
# Add the hover tool to the graph
p.add_tools(hover)
return p
#
binsize =10
A_hist = interactive_histogram(df,'A',df['A'].nunique(),[min(df['A']),max(df['A'])],'A Histogram','A','A')
B_hist = interactive_histogram(df,'B',df['B'].nunique(),[min(df['B']),max(df['B'])],'B','B ','B')
#
Graphs1 = row([A_hist,B_hist])
Controls1= column([A_Slider,B_Slider])
grid = gridplot([[Graphs1],
[Controls1]])
curdoc().add_root(grid)
show(grid)
curdoc().title = "questiontrialsample"
currently, the histograms are plot and sliders appear and when I move the sliders, the plot does not update
I tried passing an updated data frame to the function histogram and it doesnât seem to work