首页 > 解决方案 > 带有下拉菜单的散景线图未链接/白屏

问题描述

我有一个脚本可以在 python 中使用散景绘制 covid-19 死亡的折线图。我添加了一个下拉菜单,允许用户根据日期时间轴(日期)选择一个国家来绘制 new_deaths_per_million。我的问题是下拉菜单没有链接/工作。

我已经尝试根据互联网上的类似案例以不同的方式重写回调函数,但仍然无法解决问题。

我的代码如下所示:

import pandas as pd
import numpy as np
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure
from bokeh.layouts import row
from bokeh.io import curdoc
#Loading in Dataframe
kolommen=['iso_code', 'new_deaths','continent', 'location', 'date', 'total_deaths_per_million', 'new_deaths_per_million','people_vaccinated_per_hundred', 'stringency_index']
df=pd.read_csv('owid-covid-data.csv', usecols=kolommen)
# Create ColumnDataSource: source
source = ColumnDataSource(data={
    'date' : df.date,
    'new_deaths_per_million' : df.new_deaths_per_million,
    'new_deaths' : df.new_deaths,
    'location': df.location
})
# Create a new plot: plot
plot = figure(plot_height=400, plot_width=700, x_axis_type='datetime')
# Add circles to the plot
plot.line('date', 'new_deaths_per_million', source=source)
# Define a callback function: update_plot
def update_plot(attr, old, new):
    # Set the yr name to slider.value and new_data to source.data
    land = select.value
    new_data = {
        'date'    : df[df['location']==land].date,
        'new_deaths_per_million': df[df['location']==land].new_deaths_per_million,
        'new_deaths' : df[df['location']==land].new_deaths_per_million,
        'locations': df[df['location']==land].location
    }
    source= new_data
# Create a dropdown Select widget: select    
select = Select(title="Landkeuze", options=df.location.unique().tolist(), value='Netherlands')
# Attach the update_plot callback to the 'value' property of select
select.on_change('value', update_plot)
# Create layout and add to current document
layout = row(select, plot)
curdoc().add_root(layout)

我的数据框如下所示: iso_code 大陆位置日期 new_deaths total_deaths_per_million new_deaths_per_million people_vaccinated_per_hundred stringency_index AFG 亚洲阿富汗 2021-01-29 2.0 61.626 0.051 NaN 12.04 AFG 亚洲阿富汗 2021-01-30 1.0 61.6252 0.026 NaN

在此处输入图像描述

标签: pythoncallbackbokeh

解决方案


推荐阅读