【问题标题】:How do you update bokeh.plotting.figure.y_axis using bokeh server如何使用散景服务器更新 bokeh.plotting.figure.y 轴
【发布时间】:2019-02-12 06:29:48
【问题描述】:

--在我能够复制问题并进一步理解后更新。

当我为 Bokeh Server 设置我的图形时,我将 25 添加到 y_range,因此我在顶部有一些填充标签。

plot = figure(x_range=chart_data_source.data['we'],
    plot_height=250,
plot.y_range.end = max(chart_data_source.data['total']+25)

稍后,通过回调更新数据源后,我想重新设置 y_range。以下行在我的更新函数中,由任何多选小部件中的更改调用。关于我的身材的其他一切变化都很好,但 y_range 没有。我尝试更新 y_range:

plot.y_range.end = max(chart_data_source.data['total'] + 25)

不更新 y_range。知道如何在散景服务器中更新我的 y_range 吗?
散景服务器版本 1.0.4

【问题讨论】:

    标签: bokeh


    【解决方案1】:

    我知道为什么我的 y_range 没有更新。如果我独立设置 y_range.max ,那么以后我不能在 update() 函数中“重新设置”它。但是,如果我在声明图形时进行设置,那么我可以更新我的 update() 函数中的值。这是 Bokeh 的预期行为吗?

    import pandas as pd
    import numpy as np
    from bokeh.io import curdoc
    from bokeh.layouts import layout
    from bokeh.plotting import figure
    from bokeh.models import ColumnDataSource
    from bokeh.models.widgets import Button
    
    
    def populate_table_source(df):
        ds = {}
        for col in list(df):
            ds[col] = df[col]
        return ds
    
    
    colors = ["#c9d9d3", "#718dbf", "#e84d60"]
    years = ['2015', '2016', '2017']
    data = {'fruits': ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'],
            '2015': [2, 1, 4, 3, 2, 4],
            '2016': [5, 3, 4, 2, 4, 6],
            '2017': [3, 2, 4, 4, 5, 3]}
    df_chart = pd.DataFrame(data)
    df_chart['total'] = df_chart[years].sum(axis=1)
    
    print(df_chart)
    chart_data_source = ColumnDataSource(data={})
    chart_data_source.data = populate_table_source(df_chart)
    
    years = ['2015', '2016', '2017']
    # set up widgets
    button = Button(label="Update", button_type="success")
    
    
    p = figure(x_range=chart_data_source.data['fruits'],
               plot_height=250,
               plot_width=1000,
               title="Fruit Counts by Year",
               tools="hover,save",
               tooltips="$name: @$name",
               y_range=(0, 10) # can be updated if you assign it here
               )
    p.vbar_stack(years,
                 x='fruits',
                 width=0.9,
                 color=colors,
                 source=chart_data_source,
                 )
    
    # p.y_range.start = 0
    # p.y_range.end = 10 # can't be udpated if you assign it here
    
    def update_data():
        data = {'fruits': ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries'],
                '2015': [np.random.randint(5), np.random.randint(5), np.random.randint(5), np.random.randint(5),
                         np.random.randint(5), np.random.randint(5)],
                '2016': [np.random.randint(5), np.random.randint(5), np.random.randint(5), np.random.randint(5),
                         np.random.randint(5), np.random.randint(5)],
                '2017': [np.random.randint(5), np.random.randint(5), np.random.randint(5), np.random.randint(5),
                         np.random.randint(5), np.random.randint(5)],
                }
        df_chart = pd.DataFrame(data)
        df_chart['total'] = df_chart[years].sum(axis=1)
        chart_data_source.data = populate_table_source(df_chart)
        old_y_range = p.y_range.end
        p.y_range.end = old_y_range + 2
    
    
    # Set up layout
    button.on_click(update_data)
    lo = layout(button, p)
    
    curdoc().add_root(lo)
    curdoc().title = "MVE"
    

    【讨论】:

    • 对我来说似乎是一个参考错误
    【解决方案2】:

    Range1d 是一个对象,它有startend 属性,这些是你可以更新的。将一个数字指定为整个范围是没有意义的。

    plot.y_range.end = max(chart_data_source.data['total'] + 25)
    

    您看到的错误消息是由其他原因引起的,但如果没有更多代码,则无法推测。

    【讨论】:

    • 有道理,但我也试过了,但它不起作用: plot.y_range = Range1d(0, 175) y_range 保持不变,但至少我没有收到警告消息。
    • Bokeh 的最佳实践始终是尽可能进行最小的更改,即更新现有范围对象的属性(如我上面显示的方式),并且不要批量替换范围对象。如果这不起作用,您将需要发布更多代码以进行推测(实际上,您应该始终按照要求发布一个最小的复制示例)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多