如何使用选择工具更新Bokeh中的Pretext

时间:2018-06-04 12:34:14

标签: python dataframe plot bokeh

我有一个散景图,通过选择工具更新我的情节。选择工具包含更新图表的主题,其中值为x='Polarity'y='Subjectivity'

这是我想要的虚拟数据:

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)

我更新图表的代码如下:

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)

我想添加一个具有df.describe()的'Pretext',可以通过选择工具更新图表。我通过添加这些代码尝试了这一点,但它没有显示任何内容:

stats = PreText(text='', width=500)
t1 = select.value

def update_stats(df, t1):
    stats.text = str(df[[t1, select.value+'_returns']].describe())


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

show(layout)

有人知道解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您不需要两个单独的功能,您只需更改原始功能update_plot即可添加语句以将PreText的文本更改为stats.text = str(df_filter.describe())。该功能如下所示 -

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
    stats.text = str(df_filter.describe())

整个代码

from bokeh.models.widgets import Select, PreText
from bokeh.layouts import column, row
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
from bokeh.plotting import figure, show
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)

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()

stats = PreText(text=str(df.describe()), width=500)

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
    stats.text = str(df_filter.describe())


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