我正在尝试使用Bokeh基于列表创建交互式绘图,列表中的每个元素都是一个DataFrame。我想通过下拉菜单选择需要绘制的df的顺序。
我有一个包含2个DataFrame的列表,如果我选择OPTION1,则要从列表中绘制第一个df。如果我选择OPTION2,我想从列表中绘制第二个df,两者的索引都是DateTimeIndex ........对不起,这是我第一次使用Bokeh。 默认情况下,绘图始终是第一次绘图,默认情况下始终会绘图,但是当我从下拉菜单中选择新的OPTION时,绘图不会更新。我们将不胜感激帮助确定我的错误。
编辑新的源代码
import pickle
# https://stackoverflow.com/questions/58250387/bokeh-server-plot-not-updating-with-drop-down-menu-selection
from numpy.random import random
from bokeh.models import DatetimeTickFormatter, Slider, RangeTool
from bokeh.models.layouts import WidgetBox, Column, Row
from bokeh.plotting import ColumnDataSource, Figure, output_file, show, output_notebook
from bokeh.models.widgets import Select, TextInput
from bokeh.io import curdoc
from bokeh.palettes import Spectral11, Blues9, Oranges9
from bokeh.layouts import layout, row, column
from bokeh.plotting import figure
machines_produccion = [['LPOT101', 'PREN101', 'LPOT102', 'OTRO103', 'OTRO102', 'LPOT103'],
['TAGU004', 'LPOT005', 'LPOT003', 'LPOT002', 'TAGU003', 'TAGU005', 'LASE001', 'TAGU001', 'LPOT004', 'LPOT001',
'TAGU002']]
machines_aux = [['COAI109', 'COAI105', 'OTRO101', 'COAI108', 'COAI107', 'ALUM101', 'COAI104', 'COAI103', 'COAI106'],
['ALUM002', 'REFR001', 'COAI002', 'ALUM001', 'COAI001']]
LABELS_OPTION = ['OPTION1', 'OPTION2']
def get_dataset(src, idx):
df = src[idx].reset_index()
df.index.name = 'level_0'
return ColumnDataSource.from_df(data=df)
def make_plot(source, title):
TOOLS = 'crosshair,save,pan,box_zoom,reset,wheel_zoom,tap,hover'
plot = figure(plot_height=200, plot_width=600, title="NAIA4.0 PEAKS", toolbar_location=None, sizing_mode="scale_both", tools=TOOLS)#, x_range=('2018-07-05','2018-07-10'))
# plot = figure(x_axis_type="datetime", plot_width=800, tools="", toolbar_location=None)
plot.title.text = title
for machine, color in zip(machines_produccion[i], Spectral11):
plot.line(x='level_0', y=machine, source=source, line_color=color, line_width=1, legend='_' + machine,alpha=0.1)
for machine, color in zip(machines_aux[i], Spectral11):
plot.line(x='level_0', y=machine, source=source, line_color=color, line_width=2, line_dash="4 4", legend='_'+machine,alpha=0.1)
# fixed attributes
plot.legend.location = "top_right"
plot.legend.click_policy = "hide"
plot.xaxis.axis_label = 'datetime'
plot.xaxis[0].formatter = DatetimeTickFormatter()
plot.yaxis.axis_label = 'value(kW)'
return plot
def update_plot(attrname, old, new):
option_val = option_select.value
if option_val == 'OPTION1':
i=0
else:
i=1
plot.title.text = option_val
src = get_dataset(dfs, i)
source.data = src
i=0
option_select = Select(title="OPTIONS", value="OPTION1", options=LABELS_OPTION)
path_pickle = '../repo/processed/dfs_.pickle'
with open(path_pickle, 'rb') as f:
dfs = pickle.load(f)
df1 = dfs[i].reset_index()
df1.index.name = 'level_0'
source = ColumnDataSource(data = df1)
plot = make_plot(source, "TITLE")
option_select.on_change('value', update_plot)
controls = [option_select]
inputs = column(*controls, width=320, height=1000)
inputs.sizing_mode = 'fixed'
l = layout(row(inputs, plot), sizing_mode="scale_both")
curdoc().add_root(l)
在下拉菜单上选择时,我无法更新绘图。我打印“ select_option”,但情节没有更新。