使用选择根据条件更改交互式地图?

时间:2019-12-12 02:08:26

标签: python jupyter-notebook bokeh geopandas

我正在尝试使用下拉菜单选择按销售代表进行过滤,以更新地图上的数据。我发现运行代码时找不到repname。如何动态添加名称以过滤数据。我还有其他几件事可以尝试获取更新的值,但似乎没有任何效果。

def json_data(repname):
    person = repname
    df1 = map.merge(data, on='C_Name')
    df = df1[df1['SalesRep'] == person]
    df_json = json.loads(df.to_json())
    json_data = json.dumps(df_json)
    return json_data


def update_plot(attr, old, new):

    person = menu.value
    input_field = df.loc[df['SalesRep'] == cr, 'field'].iloc[0]
    p =make_plot(input_field)

    layout = column(p, widgetbox(select))
    curdoc().clear()
    curdoc().add_root(layout)
    geosource.geojson = json_data(repname)


def make_plot(field_name):  
    color_mapper = LinearColorMapper(palette=palette, low = 0, high = 400)
    color_bar = ColorBar(color_mapper = color_mapper, label_standoff =8, width = 700, height = 20, 
    border_line_color=None, location = (0,0), orientation ='horizontal')


    p = figure(title = 'Rep Total', plot_height = 700, plot_width = 700, toolbar_location = 
               None, tools =[hover])
    p.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
    p.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks

    p.yaxis.major_tick_line_color = None  # turn off y-axis major ticks
    p.yaxis.minor_tick_line_color = None 
    p.xaxis.visible = False
    p.xgrid.visible = False
    p.yaxis.visible = False
    p.ygrid.visible = False    

    p.patches('xs','ys', source = geosource,fill_color = {'field' :'Sales', 'transform' : 
              color_mapper},
            line_color = 'black', line_width = 0.25, fill_alpha = 1)
    p.add_layout(color_bar, 'below')

    p.add_tools(hover)
    return p

    #target = show(p, notebook_handle=True)

geosource = GeoJSONDataSource(geojson=json_data(can))
input_field = 'Jim'
palette = brewer['Reds'][6]

palette = palette[::-1]

hover = HoverTool(tooltips = [('State','@StateName'),('Rank','@{Sales Rank}'),('No. of 
                               Sales','@SalesCount')])

p = make_plot(input_field)

menu = Select(title='Which Candidate', value='Jim', options=['Jim', 'Mike', 
             'Robert', 'Sally])

menu.on_change('value',update_plot)
layout = column(p, widgetbox(menu))
curdoc().add_root(layout)
output_notebook()
show(p)

0 个答案:

没有答案