I am trying to incorporate some bokeh
widgets with pandas_bokeh
plots within a dashboard in order to interactively update them through filtering. However, when I launch the dashboard, I get a blank browser tab. Here is my code:-
import numpy as np
import pandas as pd
import pandas_bokeh
from bokeh.io import show, curdoc
from bokeh.layouts import column, layout, row
from bokeh.models import DateRangeSlider, Dropdown
# Embedding plots in Jupyter/Colab Notebook
#pandas_bokeh.output_notebook()
# for exporting plots as HTML
#pandas_bokeh.output_file(filename)
#color palette
colors=['#FDE724','#D01C8B','#4DAC26']
#define the categorical variable
category_a = ['A','B','C']
category_b = ['X','Y','Z']
#set random seed to make the dataset reproducible
np.random.seed(42)
#create a dataset
#df_random = pd.DataFrame({
# 'id': np.arange(0, 15),
# 'month':np.random.randint(1, 12, 15),
# 'sensor_1': np.random.uniform(0, 1,15),
# 'sensor_2': np.random.uniform(10, 15, 15),
# 'sensor_3': np.random.randint(0, 20, 15),
# 'category': np.random.choice(category, 15, p=[0.2, 0.4, 0.4]),
#})
df_random2 = pd.DataFrame({
'id': np.arange(0, 30),
'date': pd.date_range(start='1/1/2021', periods=30, freq='D'),
'month':np.random.randint(1, 12, 30),
'sensor_1': np.random.uniform(0, 1,30),
'sensor_2': np.random.uniform(10, 15, 30),
'sensor_3': np.random.randint(0, 20, 30),
'categorya': np.random.choice(category_a, 30, p=[0.2, 0.4, 0.4]),
'categoryb': np.random.choice(category_b, 30, p=[0.6, 0.2, 0.2]),
})
#set index to id column
df_random2=df_random2.set_index('id')
df_random2.shape
df_random2.head()
mindate=df_random2['date'].min()
maxdate=df_random2['date'].max()
menucols = [("Category A", "categorya"),("Category B","categoryb")]
date_range_slider = DateRangeSlider(value=(mindate, maxdate),
start=mindate, end=maxdate)
category_picker = Dropdown(label = "Column select", button_type="default", menu=menucols)
# Plot1 - Line plot
p_line= df_random2.groupby(['date']).mean().plot_bokeh(kind="line",y="sensor_2",color='#d01c8b',plot_data_points=True,show_figure=False)
# Plot2- Barplot
p_bar = df_random2.groupby(['date']).mean().plot_bokeh(kind="bar",y='sensor_2', colormap=colors,show_figure=False)
# Plot3- stacked bar chart
df_sensor=df_random2.drop(['date','month','categorya'],axis=1)
p_stack=df_sensor.groupby(['categoryb']).mean().plot_bokeh(kind='barh', stacked=True,colormap=colors,show_figure=False)
#Plot4- Scatterplot
p_scatter = df_random2.plot_bokeh(kind="scatter", x="month", y="sensor_2",category="categoryb",colormap=colors,show_figure=False)
#Plot5- Pie chart
p_pie= df_random2.groupby(['categoryb']).mean().plot_bokeh.pie(y='sensor_1',colormap=colors,show_figure=False)
#Plot6- Histogram
p_hist=df_sensor.plot_bokeh(kind='hist', histogram_type="stacked",bins=6,colormap=colors, show_figure=False)
widgets = [date_range_slider,category_picker]
#Make Dashboard with Grid Layout:
plots=pandas_bokeh.plot_grid([[widgets],[p_line, p_bar,p_stack],[p_scatter, p_pie,p_hist]], plot_width=400)
#show(bokehlayout)
curdoc().add_root(plots)
curdoc().title = "layout"
And here is the error message I get:-
ValueError: Only LayoutDOM items can be inserted into a column. Tried to insert: [DateRangeSlider(id='1002', ...), Dropdown(id='1003', ...)] of type <class 'list'>
Can someone help me understand why I am getting this message and perhaps can I politely ask someone to suggest some edits to my code to get this to work?
CodePudding user response:
man, pandas_bokeh use for basic, mini thinks. if you want to graph right away, use it. its effective. but if you want to use other thinks such as widgets, tools etc. use bokeh itself. message came cause of you need to use bokeh layouts. pandas_bokeh don't support everthing, it's 3rd party add-on.
for your code from bokeh.layouts import layout
and
plots = layout([[widgets],[p_line, p_bar,p_stack],[p_scatter, p_pie,p_hist]])
curdoc().add_root(plots)