基于bokeh网站画廊上提供的示例,我添加了一个下拉小部件。下拉值映射到不同类别的列表。 我想要update_data_plot函数要做的是更新这些级别并显示新的发行版。
from numpy import linspace
from scipy.stats.kde import gaussian_kde
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, FixedTicker, PrintfTickFormatter
from bokeh.plotting import figure
from bokeh.sampledata.perceptions import probly
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox, column
from bokeh.models.widgets import Slider, TextInput, Dropdown
# widgets
menu = [('A','A'), ('B','B')]
dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
def ridge(category, data, scale=20):
return list(zip([category]*len(data), scale*data))
def update_data_plot(attrname, old, new):
"""Update the chart to show
the new categorical levels and
their distribution"""
cats1 = list(reversed(probly.keys()))[:5]
cats2 = list(reversed(probly.keys()))[5:]
x = linspace(-20,110, 500)
source = ColumnDataSource(data=dict(x=x))
p = figure(y_range=cats1, plot_width=900, x_range=(-5, 105), toolbar_location=None)
for i, cat in enumerate(reversed(cats1)):
pdf = gaussian_kde(probly[cat])
y = ridge(cat, pdf(x))
source.add(y, cat)
p.patch('x', cat, alpha=0.6, line_color="black", source=source)
p.outline_line_color = None
p.background_fill_color = "#efefef"
p.xaxis.ticker = FixedTicker(ticks=list(range(0, 101, 10)))
p.xaxis.formatter = PrintfTickFormatter(format="%d%%")
p.ygrid.grid_line_color = None
p.xgrid.grid_line_color = "#dddddd"
p.xgrid.ticker = p.xaxis[0].ticker
p.axis.minor_tick_line_color = None
p.axis.major_tick_line_color = None
p.axis.axis_line_color = None
p.y_range.range_padding = 0.12
dropdown.on_change('value',update_data)
curdoc().add_root(column(dropdown,
p, width=50))
curdoc().title = "Sliders"
答案 0 :(得分:0)
自从我做了这样的事情已经有一段时间了。我是在本地bokeh服务应用程序上执行此操作的,我认为我从来没有在笔记本上运行它。查看我的旧代码,用新的source.data
的{{1}}更新.data
可以正常工作。
ColumnDataSource