【问题标题】:Bokeh server plot does not update散景服务器绘图未更新
【发布时间】:2020-02-09 20:14:12
【问题描述】:

LS, 关于同一主题的所有答案都没有帮助我解决我的更新问题。我认为这与 dfs = df.sort_values(by=['E']) 有关。我使用所有最新版本的库。散景网站上的示例在我的配置上运行良好。通过更新按钮,我想允许用户选择首选的排序顺序。当这部分工作时,将添加另外两个排序按钮。 这是我的代码:

import pandas as pd
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.layouts import gridplot
from bokeh.models import Button

df = pd.DataFrame(dict(A=["AMS", "LHR", "FRA", "PTY", "CGD"], S=[7,-5,-3,3,2], E=[8,3,-2,5,8], C=[5,2,7,-3,-4]))

source = ColumnDataSource(df)

options = dict(plot_width=300, plot_height=200,
               tools="pan,wheel_zoom,box_zoom,box_select,lasso_select")

button = Button(label="Change Sort to E")

p1 = figure(y_range=source.data['A'].tolist(), title="S", **options)
p1.hbar(y='A', right="S", height=0.2, source=source)


p2 = figure(y_range=source.data['A'].tolist(), title="E", **options)
p2.hbar(y="A", right="E", height=0.2, source=source)


p3 = figure(y_range=source.data['A'].tolist(), title="C", **options)
p3.hbar(y="A", right="C", height=0.2, source=source)


def update():
    dfs = df.sort_values(by=['E'])
    source.data = ColumnDataSource.from_df(dfs)


button.on_click(update)

p = gridplot([[button], [p1, p2, p3]], toolbar_location="right")

curdoc().add_root(p)

我通过以下方式运行服务器:bokeh serve --show app.py --port 5009

非常感谢您进行更新。

【问题讨论】:

    标签: server bokeh


    【解决方案1】:

    如果要更改分类轴上的事物顺序,则必须更新 范围。轴上因子的顺序完全由您为范围配置的因子顺序指定,因此您需要重新排序因子以匹配所需的排序顺序。所以,类似:

    p1.y_range.factors = new_sorted_factors
    

    见

    https://docs.bokeh.org/en/latest/docs/user_guide/categorical.html#sorted

    对于一个完整的(独立)示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多