如何基于下拉选择更新散景图?

时间:2019-12-02 12:18:10

标签: python pandas bokeh

我制作了一张图表,显示了一段时间内的真空价格(我有一个抓取并获取价格的不同文件)。我用散景制作这张图。我开始抓取不同类型的吸尘器(机器人与立式吸尘器),并希望添加一个下拉列表,以便视图可以选择他们正在查看的吸尘器类别。这是我到目前为止的代码:

import pandas as pd
import os
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.palettes import Viridis3
from bokeh.models import HoverTool, Select
from bokeh.layouts import column

# not real file path
GENERAL_PATH = 'C:\\Users\\data'

def make_plot(input_category):
    p = figure(plot_width=600,
               plot_height=600,
               title='Vacuum Prices Over Time',
               x_axis_label='Date',
               y_axis_label='Price',
               x_axis_type='datetime')
    p.background_fill_color = '#f5f5f5'

    for brand, color in zip(robot_df.brand.unique(), Viridis3):
        temp1_df = df.loc[(df['brand'] == brand) & (df['category'] == input_category)]
        p.circle(temp1_df['date'], temp1_df['price'], line_color=color)
        p.line(temp1_df['date'], temp1_df['price'], line_width=3, color=color, alpha=0.8, legend_label=brand)

    p.legend.location = 'top_left'
    p.legend.click_policy = 'hide'
    return p

def update_plot(attr, old, new):
    category = vacuum_select.value
    make_plot(category)

# get the full paths to data files
filenames = os.listdir(GENERAL_PATH)
files_with_path = []
for file in filenames:
    files_with_path.append(GENERAL_PATH + '\\' + file)


# get data from each file and create dataframe
temp = []
for file in files_with_path:
    temp_df = pd.read_csv(file, encoding='ISO-8859-1', header=None)
    temp.append(temp_df)
df = pd.concat(temp, axis=0, ignore_index=True)
df.columns = ['date', 'name', 'brand', 'retailer', 'price', 'stars', 'category', 'highlights']
df['date'] = pd.to_datetime(df['date'])


#create dataframes for the vacuum categories
upright_df = df[(df['category']) == 'Upright']
robot_df = df[(df['category']) == 'Robot']

#make initial plot, will be robot by default
plot = make_plot('Robot')
vacuum_select = Select(title='Vacuum Category', options=['Robot', 'Upright'], value='Robot')
vacuum_select.on_change('value', update_plot)

show(column(vacuum_select, plot))

现在,on_change行正在生成一条错误消息

:WARNING:bokeh.embed.util:
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:

    https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html

Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:

    https://docs.bokeh.org/en/latest/docs/user_guide/server.html

我不确定在不使用on_change的情况下还能如何获得更改,并且我对update_plot函数中应包含的内容也感到困惑。预先谢谢您,我们将为您提供任何帮助!

0 个答案:

没有答案