散景:按钮单击无法正确更新vbar

时间:2019-08-07 19:54:32

标签: python-3.x pandas bokeh

我试图在用户单击按钮后进行vbar绘图更新,但是当它被多次击中时,它不能正常工作。我的代码会随机生成一个10个字母的列表,其数量为'a','b'或'c',并且每次按下按钮时都应绘制正确的数量。

下面显示的代码使用Python 3.7.2Bokeh 1.3.1

from bokeh.io import curdoc
from bokeh.layouts import row
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.models.widgets import Button
from bokeh.plotting import figure

import pandas as pd
import numpy as np

plot_figure = figure(title='Button click', x_range=FactorRange())

button = Button(label="Click to set plot title", button_type="success")

def button_click():
    i = ['a', 'b', 'c']
    x = np.random.choice(i, 10)

    df = pd.DataFrame({'x': x})
    grouped_df = df.groupby('x').size().reset_index(name='counts')

    print(grouped_df.counts)

    plot_figure.vbar(x=grouped_df.x, top=grouped_df.counts, width=0.9)
    plot_figure.x_range.factors = grouped_df.x.values.tolist()
    plot_figure.title.text = 'Button Clicked'


button.on_click(button_click)

layout = row(button, plot_figure)

curdoc().add_root(layout)
curdoc().title = "Button Bokeh Server"

运行bokeh serve --show <filename>并单击按钮后,我得到正常结果,如下图所示:

enter image description here

a有7,b有1,c有2。

但是当我再次单击该按钮时,a,b和c的值分别更改为4、3、3,但是图像无法正确更改:

enter image description here

条形图的高度应为4、3和3,但它们的高度分别为7、3、3。它们似乎仅取先前单击中发生的最大值。此模式继续单击按钮“ x”多次。作为没有大量构建Bokeh应用程序经验的人,我该如何解决此问题,以便在每次单击后重新设置DataFrame的值?

0 个答案:

没有答案