【发布时间】:2018-11-09 05:08:21
【问题描述】:
我一直在 stackoverflow 上关注一些示例,以使用“选择”小部件更新我的绘图。当我通过 anaconda shell 运行 .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)
经过多次不同的尝试,我仍然无法让交互正常工作。我做错了什么?
【问题讨论】:
-
我看到你有
output_file和show。这些通常与静态 HTML 和 JS 的 独立 Bokeh 文档一起使用,并且不适用于真正的 Pythonon_change回调。您是像普通的 python 脚本一样运行它还是使用例如运行它bokeh serve --show app.py? -
我使用的是 bokeh serve --show app.py,但我最终修复了它!我将在下面添加解决方案。
标签: python plot bokeh interactive