这是你想要的吗?注意,我没有使用从导入的Bar函数博克图表因为这不会在更新数据源时更新。
如果你想坚持使用酒吧博克图表每次都需要重新创建绘图。在
注意:要运行这个并进行更新工作,您需要从命令行执行bokeh serve show plotfilename.py。在from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models.widgets import Button
from bokeh.plotting import ColumnDataSource, figure
import random
def bar_plot(fig, source):
fig.vbar(x='x', width=0.5, bottom=0,top='y',source=source, color="firebrick")
return fig
def update_data():
data = source.data
data['y'] = random.sample(range(0,10),len(data['y']))
source.data =data
button = Button(label="Press here to update data", button_type="success")
button.on_click(update_data)
data = {'x':[0,1,2,3],'y':[10,20,30,40]}
source = ColumnDataSource(data)
fig = figure(plot_width=650,
plot_height=500,
x_axis_label='x',
y_axis_label='y')
fig = bar_plot(fig, source)
layout = layout([[button,fig]])
curdoc().add_root(layout)
编辑:请参阅下面的一个方法,该方法可以绘制bokeh图,但可以根据需要使用数据帧中的数据。它还将在每次按下按钮时更新绘图。仍然需要使用命令bokeh serve show plotfilename.py
^{pr2}$