Python bokeh select小部件不更新绘图

Python bokeh select小部件不更新绘图,python,plot,bokeh,interactive,Python,Plot,Bokeh,Interactive,在stackoverflow,我一直在遵循一些示例,使用“选择”小部件更新我的绘图。当我通过AnacondaShell运行.py文件时,我可以看到绘图和“选择”小部件。然而,不知何故,我的绘图并没有更新绘图。我必须说,数据集的计数约为11000行,我不知道这是否相关。我看到了一个主题,其中将数据帧转换为字典有助于实现交互。因此,我使用以下代码实现了这一点: from bokeh.layouts import row, column, widgetbox from bokeh.plotting i

在stackoverflow,我一直在遵循一些示例,使用“选择”小部件更新我的绘图。当我通过AnacondaShell运行.py文件时,我可以看到绘图和“选择”小部件。然而,不知何故,我的绘图并没有更新绘图。我必须说,数据集的计数约为11000行,我不知道这是否相关。我看到了一个主题,其中将数据帧转换为字典有助于实现交互。因此,我使用以下代码实现了这一点:

from bokeh.layouts import row, column, widgetbox
from bokeh.plotting import figure, show, output_file, ColumnDataSource
from bokeh.models.widgets import Select
from bokeh.io import curdoc, show

df = pd.read_excel('data.xlsx')

d1 = df.to_dict()
d2 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'compliment'].to_dict()
d3 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'complaint'].to_dict()
d4 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'infrastructure'].to_dict()
d5 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'autority'].to_dict()
d6 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'finance'].to_dict()
d7 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'english'].to_dict()
d8 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'danger'].to_dict()
d9 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'health'].to_dict()
d10 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'sport'].to_dict()
d11 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'remaining'].to_dict()
source = ColumnDataSource(df)

p = figure()
r = p.circle(x='Polarity', y='Subjectivity', 
         source = source)

select = Select(title="Subject",  options=['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11'])

def update_plot(attr, old, new):

    if select.value == 'd1': 
        newSource = d1

    if select.value == 'd2':
        newSource = d2
    if select.value == 'd3':
        newSource = d3
    if select.value == 'd4':
        newSource = d4
    if select.value == 'd5':
        newSource = d5
    if select.value == 'd6':
        newSource = d6
    if select.value == 'd7':
        newSource = d7
    if select.value == 'd8':
        newSource = d8
    if select.value == 'd9':
        newSource = d9
    if select.value == 'd10':
        newSource = d10
    if select.value == 'd11':
        newSource = d11
    source.data =  newSource 

select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
curdoc().add_root(layout)
现在我已经制作了字典,我用以下代码绘制了图:

from bokeh.layouts import row, column, widgetbox
from bokeh.plotting import figure, show, output_file, ColumnDataSource
from bokeh.models.widgets import Select
from bokeh.io import curdoc, show

df = pd.read_excel('data.xlsx')

d1 = df.to_dict()
d2 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'compliment'].to_dict()
d3 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'complaint'].to_dict()
d4 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'infrastructure'].to_dict()
d5 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'autority'].to_dict()
d6 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'finance'].to_dict()
d7 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'english'].to_dict()
d8 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'danger'].to_dict()
d9 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'health'].to_dict()
d10 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'sport'].to_dict()
d11 = df[['Polarity', 'Subjectivity']].loc[df['Subject'] == 'remaining'].to_dict()
source = ColumnDataSource(df)

p = figure()
r = p.circle(x='Polarity', y='Subjectivity', 
         source = source)

select = Select(title="Subject",  options=['d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'd10', 'd11'])

def update_plot(attr, old, new):

    if select.value == 'd1': 
        newSource = d1

    if select.value == 'd2':
        newSource = d2
    if select.value == 'd3':
        newSource = d3
    if select.value == 'd4':
        newSource = d4
    if select.value == 'd5':
        newSource = d5
    if select.value == 'd6':
        newSource = d6
    if select.value == 'd7':
        newSource = d7
    if select.value == 'd8':
        newSource = d8
    if select.value == 'd9':
        newSource = d9
    if select.value == 'd10':
        newSource = d10
    if select.value == 'd11':
        newSource = d11
    source.data =  newSource 

select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
curdoc().add_root(layout)

在多次尝试之后,我仍然无法让交互正常工作。我做错了什么?

由于您没有提供数据,我使用以下代码创建了虚拟数据-

import pandas as pd
import random

list_type = ['All', 'Compliment', 'Sport', 'Remaining', 'Finance', 'Infrastructure', 'Complaint', 'Authority',
 'Danger', 'Health', 'English']



df = pd.concat([pd.DataFrame({'Subject' : [list_type[i] for t in range(110)], 
                   'Polarity' : [random.random() for t in range(110)],
                   'Subjectivity' : [random.random() for t in range(110)]}) for i in range(len(list_type))], axis=0)
您需要使用与图表关联的数据源。您可以使用简单的函数操作dataframe,创建columndatasource并更改图表后面的数据-

options = []
options.append('All')

options.extend(df['Subject'].unique().tolist())
source = ColumnDataSource(df)

p = figure()
r = p.circle(x='Polarity', y='Subjectivity', source = source)

select = Select(title="Subject",  options=options, value="All")
output_notebook()

def update_plot(attr, old, new):
    if select.value=="All":
        df_filter = df.copy()
    else:
        df_filter = df[df['Subject']==select.value]
    source1 = ColumnDataSource(df_filter)
    r.data_source.data = source1.data
select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
#show(layout)
curdoc().add_root(layout)

我看到你有输出文件和显示。这些文档通常与静态HTML和JS的独立Bokeh文档一起使用,而不与真正的Python一起使用。您是像运行普通python脚本一样运行此脚本,还是使用bokeh serve-show app.py来运行它?我使用的是bokeh serve-show app.py,但我最终修复了它!我将在下面添加解决方案。您的anwser看起来比我的解决方案更干净。如果主题为空,这会防止错误吗?假设“体育”这个主题没有“极性”和“主观性”。我的互动情节还能用吗?先生,你真棒!这是更清洁,它的工作!多谢各位