简体   繁体   English

无法在Bokeh python中更新数据源

[英]Unable to update datasource in Bokeh python

I am using Bokeh 1.0.1. 我正在使用Bokeh 1.0.1。 I am unable to update the data source in the Update method ie src.data.update(new_src.data) doesn't seem to work. 我无法在Update方法中更新数据源,即src.data.update(new_src.data)似乎不起作用。 Below is the full code. 下面是完整的代码。

def modify_doc(doc):

    def create_dataset(df, resample='D'):
        # Resample the data
        src = df.resample(resample).mean()

        # Reset index for hovering
        src.reset_index(inplace=True)

        return ColumnDataSource(src) 

    def create_plot(src):
        # Blank plot with correct labels
        p = figure(plot_width=700, plot_height=300, x_axis_type="datetime",
                  title = 'Variation of Pollution',
                  x_axis_label = 'Time', y_axis_label = 'Pollution (µg/m³)')

        p.line(source=src, x='Date & Time', y='pm2.5', line_width=2,
               color='firebrick', line_alpha=0.5, legend='Actual')

        hover = HoverTool(tooltips=[('Pollution', '@{pm2.5} µg/m³'),
                                    ('Air Temp', '@{Air Temp.} °C'),
                                    ('Temp', '(@{Min. Temp.}{0.2f}, @{Max. Temp.}{0.2f}) °C'),
                                    ('Dew Pt.', '@{Dew Pt.} °C'),
                                    ('Rainfall', '@Rainfall mm'),
                                    ('Wind Dir.', '@{Wind Dir.} °'),
                                    ('Wind Speed', '@{Wind Speed} km/hr'),
                                    ('Relative humidity', '(@{Min. RH}{0.2f}, @{Max. RH}{0.2f}) %')],
                                     mode='vline')

        p.add_tools(hover)
        p.legend.click_policy = 'hide'

        return p

    # Update function takes three default parameters
    def update(attr, old, new):
        # Resampling list
        re_list = ['D', 'W', 'M', 'A']

        # Make a new dataset based on the selected carriers and the 
        # make_dataset function defined earlier
        new_src = create_dataset(df,
                               resample = re_list[resample_button_group.active]) 


        # Update the source used the quad glpyhs
        src.data.update(new_src.data)



    resample_button_group = RadioButtonGroup(labels=["Day", "Week", "Month", "Year"], active=1)
    resample_button_group.on_change('active', update)

    controls = WidgetBox(resample_button_group)

    # Initial Plot
    src = create_dataset(df)
    p = create_plot(src.data)

    layout = row(controls, p)
    doc.add_root(layout)

# Set up an application
handler = FunctionHandler(modify_doc)
app = Application(handler)

You should be able to update the line glyph directly. 您应该能够直接更新线字形。

First, modify your plotting code to assign a name to the line glyph: 首先,修改绘图代码,为线条字形分配名称:

pm_line = p.line(
    source=src,
    x='Date & Time', 
    y='pm2.5', 
    line_width=2,
    color='firebrick', 
    line_alpha=0.5, 
    legend='Actual',
    name='pm_line' # Add this!
)

Then in your update function, replace your existing update line with the following: 然后在您的更新功能中,将您现有的更新行替换为以下内容:

pm_line = p.select_one({'name':'pm_line'})
pm_line.data_source.data = new_src.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM