要在Bokeh中實現圖表的動態選擇和過濾器,可以使用Bokeh的widgets和回調函數來實現。以下是一個簡單的示例:
from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource
import pandas as pd
data = {
'x': [1, 2, 3, 4, 5],
'y': [6, 7, 2, 4, 5],
'color': ['red', 'blue', 'green', 'yellow', 'orange']
}
df = pd.DataFrame(data)
source = ColumnDataSource(df)
p = figure()
p.circle('x', 'y', color='color', source=source)
from bokeh.models import Select
select = Select(title='Color', options=['All'] + df['color'].unique().tolist(), value='All')
def update_plot(attrname, old, new):
if select.value == 'All':
new_data = df
else:
new_data = df[df['color'] == select.value]
source.data = ColumnDataSource.from_df(new_data)
select.on_change('value', update_plot)
from bokeh.layouts import column
output_file('filter.html')
layout = column(select, p)
show(layout)
現在,您可以在選擇器中選擇顏色,并動態過濾圖表中顯示的數據。