1 回答

TA贡献1998条经验 获得超6个赞
如果您提供一个自包含的最小示例(我们可以复制、粘贴和运行的代码),会更容易提供帮助。话虽如此,这里有一些可以帮助您入门的内容。您已经非常接近此解决方案,但我想重要的部分是为每个菜单设置一个回调。
我希望这有帮助 :-)
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from bokeh.layouts import row, widgetbox
from bokeh.models import CustomJS, Select
from bokeh.plotting import figure, show, ColumnDataSource
# Define some random data
dataframe = pd.DataFrame({
'Difference': np.sin(np.linspace(0, 100, 500)),
'Price': np.cos(np.linspace(0, 100, 500)),
'Metacritic': np.sin(np.linspace(0, 100, 500)),
'Rotten Tomatoes': np.cos(np.linspace(0, 200, 500)),
})
# Set x and y-axis defaults
dataframe['x'] = dataframe['Difference']
dataframe['y'] = dataframe['Metacritic']
# Create Bokeh's ColumnDataSource
source = ColumnDataSource(data=dataframe)
# Create the plot figure
plot = figure(plot_width=400, plot_height=400)
plot.circle('x', 'y', source=source)
# Create the dropdown menus
x_menu = Select(options=['Difference', 'Price'],
value='Difference',
title='What do you want to put on the x axis')
y_menu = Select(options=['Metacritic', 'Rotten Tomatoes'],
value='Metacritic',
title='What do you want to put on the y axis')
# Create two callbacks, one for each menu
callback_x = CustomJS(args=dict(source=source), code="""
console.log('changed selected option', cb_obj.value)
var data=source.data
data['x']=data[cb_obj.value]
source.change.emit();
""")
callback_y = CustomJS(args=dict(source=source), code="""
console.log('changed selected option', cb_obj.value)
var data=source.data
data['y']=data[cb_obj.value]
source.change.emit();
""")
# Assign callbacks to menu widgets
x_menu.callback = callback_x
y_menu.callback = callback_y
# Show the html document with a layout
show(row(widgetbox(x_menu, y_menu), plot))
添加回答
举报