如何获取bokeh小部件与情节.on_change()进行交互

时间:2019-12-11 04:45:42

标签: python bokeh

我正在尝试创建一条包含3条线的图形,数据框中的每一列一条。我希望能够在窗口小部件中选择可视化中显示的行。小部件附带了该图,但是在小部件中选择列不会影响可视化。我认为我的错误在于我的更新功能中,但我不知道该如何更新情节。

picture of plot

dataframe head

# Load in flights and inspect
data = pd.read_csv('project_data.csv', index_col='DATE', parse_dates=True)

# These are the column names
available_people = ['merissa_steps', 'sherida_steps', 'tova_steps']

def modify_doc(doc):

    def make_dataset(person_list):

        by_person = data[person_list]

        return ColumnDataSource(by_person)

    def make_plot(src, people):
        # Blank plot with correct labels
        p = figure(x_axis_type="datetime", title="Everyone's Steps", plot_height=200, plot_width=500)
        for person in people:
            p.line(source = src, x = 'DATE', y = person, color='gray')

        return p

    # Update function takes three default parameters
    def update(attr, old, new):
        # Get the list of carriers for the graph
        people_to_plot = [person_selection.labels[i] for i in 
                            person_selection.active]

        # Make a new dataset based on the selected carriers and the 
        # make_dataset function defined earlier
        new_src = make_dataset(people_to_plot)

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

    person_selection = CheckboxGroup(labels=available_people, active = [0, 1])
    person_selection.on_change('active', update)

    controls = WidgetBox(person_selection)

    initial_people = [person_selection.labels[i] for i in person_selection.active]

    src = make_dataset(initial_people)

    p = make_plot(src, initial_people)

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

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

1 个答案:

答案 0 :(得分:0)

我解决了自己的问题! src.data.update(new_src.data) 仅更新行,不会更改列。 pd.melt(...)从头开始也是这样,所以我想要的更新是逐行更新,而不是逐列更新,现在我的程序按照我想要的方式运行。