我正在Bokeh布局上使用select X属性投影两个不同的值。
SO上有类似的帖子,告诉您将from bokeh.layouts import row
更改为from bokeh.layouts import column
,并将layout = row(select_widget, plot)
更改为layout = column(select_widget, plot)
。我试过了,但是什么也没发生。
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 = pd.read_csv("C:\Python\SpendMap.csv", encoding = "ISO-8859-1")
df_construction = df[df['SubCategory'] == 'Constr']
source = ColumnDataSource(data={
'x' : df_construction['Year'],
'y' : df_construction['Spend'],
'x1': df_construction['Plant']
})
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':
data.data = {'x' : df_construction['Year'], 'y': df_construction['Spend']}
else:
data.data = {'x': df_construction['Plant'], 'y': df_construction['Spend']}
select_widget.on_change('value',callback)
layout = row(select_widget, plot)
curdoc().add_root(layout)
当运行bokeh服务--show Plant.py时,浏览器应打开,并在我选择下拉式布局但仅显示“年份”信息时向我显示散景中的Plant和Year。
答案 0 :(得分:0)
您正在尝试编辑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