python - 带有下拉菜单的散景线图未链接/白屏
问题描述
我有一个脚本可以在 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
解决方案
推荐阅读
- vue.js - 图标在表格列内停止工作
- laravel - Laravel centos vs ubuntu 性能
- php - 在laravel 5.6中单击导航栏时如何使其处于活动状态?
- reactjs - 反应重新渲染组件但不是整个页面问题
- php - 如何访问外部核心 php 文件中的 codeigniter 会话值
- android - JSONArray 无法解析
- c++ - 如何在c ++中旋转向量中的每个第N个元素
- web-scraping - 从维基词典中提取词源信息
- javascript - 如何在我的 jQuery 'each' 函数及其所有包含的 AJAX 调用之后触发我的函数?
- sql-server - 将分数存储在字符串中,无需 SQL 将其转换为日期序列