Вы пытаетесь редактировать data.data. Вы должны отредактировать source.data, так как это источник, который вы используете для глифов. Я не смог воспроизвести Модели не должны быть корневая ошибка документа.
#!/usr/bin/python3
import pandas as pd
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Select
from bokeh.io import curdoc, show
from bokeh.layouts import row
df_construction = pd.DataFrame.from_dict({'x': [2000, 2001, 2002, 2003, 2004], 'y': [50, 60, 70, 80, 90], 'x1': [2004, 2003, 2002, 2001, 2000]})
source = ColumnDataSource(df_construction)
plot = figure()
plot.diamond('x','y', source=source, color ='red')
select_widget = Select(options = ['Plant','Year'], value='Year', title = 'select new x axis attribute')
def callback(attr, old, new):
if new == 'Plant':
source.data = {'x' : df_construction['x1'], 'y': df_construction['y'], 'x1': df_construction['x']}
else:
source.data = {'x' : df_construction['x'], 'y': df_construction['y'], 'x1': df_construction['x1']}
select_widget.on_change('value',callback)
layout = row(select_widget, plot)
curdoc().add_root(layout)
Код для ноутбука Jupyter (протестирован с Bokeh 1.0.3 и 1.0.4):
#!/usr/bin/python3
import pandas as pd
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Select
from bokeh.io import curdoc, show, output_notebook
from bokeh.layouts import row
output_notebook()
def modify_doc(doc):
df_construction = pd.DataFrame.from_dict({'x': [2000, 2001, 2002, 2003, 2004], 'y': [50, 60, 70, 80, 90], 'x1': [2004, 2003, 2002, 2001, 2000]})
source = ColumnDataSource(df_construction)
plot = figure()
plot.diamond('x','y', source=source, color ='red')
select_widget = Select(options = ['Plant','Year'], value='Year', title = 'select new x axis attribute')
def callback(attr, old, new):
if new == 'Plant':
source.data = {'x' : df_construction['x1'], 'y': df_construction['y'], 'x1': df_construction['x']}
else:
source.data = {'x' : df_construction['x'], 'y': df_construction['y'], 'x1': df_construction['x1']}
select_widget.on_change('value',callback)
layout = row(select_widget, plot)
doc.add_root(layout)
show(modify_doc, notebook_url='localhost:8891') #Change this if it refuses websocket connection