如果您想以交互方式更新内容,那么最好使用bokeh.plotting API。由于一些相当无趣的技术原因,bokeh.charts
API(包括Donut
)不太适合需要就地更新内容的用例。
With bokeh.plotting
有一个wedge字形法您可以使用它来绘制饼图。这是一个完整的示例(使用 Bokeh0.12.5
)用滑块更新饼图:
from math import pi
from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.plotting import figure
output_file("pie.html")
source = ColumnDataSource(data=dict(
start=[0, 0.2], end=[0.2, 2*pi], color=['firebrick', 'navy']
))
plot = figure()
plot.wedge(x=0, y=0, start_angle='start', end_angle='end', radius=1,
color='color', alpha=0.6, source=source)
slider = Slider(start=.1, end=1., value=.2, step=.1, title="delta-V")
def update(source=source, slider=slider, window=None):
data = source.data
data['end'][0] = slider.value
source.trigger('change')
slider.js_on_change('value', CustomJS.from_py_func(update))
show(column(slider, plot))
它比Donut
版本,但是python端和JS端的数据结构之间的关系更加清晰和直接。