我是初学者。我想创建一个数字,显示世界各地不同能源类型的生产情况。我在数据中使用了来自我们世界的两个数据集,我想制作一个下拉菜单,让您可以更改能源来源。我已经画出了一种能源,我已经创建了下拉菜单,但它还不起作用
我如何包括一个下拉菜单,使行变为显示来自另一列的数据(保留悬停工具和彩色行)?
数据来源: https://ourworldindata.org/grapher/nuclear-energy-generation?stackMode=absolute&time=2017®ion=World&; https://ourworldindata.org/grapher/fossil-fuel-production?country=~OWID_WRL
from bokeh.io import show, output_file
from bokeh.plotting import figure, ColumnDataSource, curdoc
from bokeh.palettes import Category20_20
from bokeh.layouts import row
from bokeh.models import HoverTool, Select
import pandas as pd
#import datasets
df = pd.read_csv('nuclear-energy-generation.csv')
df_fossil_production = pd.read_csv('fossil-fuel-production.csv')
df = df.rename(columns={"Electricity from nuclear (TWh)": "TWh"})
#Filter for few countries for simplicity (temporary)
df = df.loc[df['Entity'].isin(["Netherlands", 'France', 'Belgium', 'Germany', 'Spain'])]
df_fossil_production = df_fossil_production.loc[df_fossil_production['Entity'].isin(["Netherlands",'France', 'Belgium', 'Germany', 'Spain'])]
#merge dataframes and convert to datetime
df_merge = pd.merge(df, df_fossil_production, on=["Entity", "Code", "Year"], how = 'outer')
df_merge["Datetime"] = pd.to_datetime(df_merge.Year, format='%Y')
#create figure for line
p = figure(title="Power generation",
plot_width=1000,
plot_height=250,
x_axis_type='datetime',
x_axis_label="Year",
y_axis_label="Power generation (TWh)")
#plot the lines for nuclear energy production
for (name, group), color in zip(df_merge.groupby('Entity'), Category20_20):
source = ColumnDataSource(group)
p.line(x='Datetime', y='TWh', source=source, legend_label=str(name), color=color)
#create definition for the dropdown box
def callback(attr, old, new):
if new == 'Nuclear':
source.data = {
'x' : df_merge["Year"],
'y' : df_merge["TWh"]}
else:
source.data = {
'x' : df_merge["Year"],
'y' : df_merge["Coal Production - EJ"]}
#create dropdown menu
select = Select(title="Energy type", options=['Nuclear', 'Fossil'], value='Nuclear')
select.on_change('value', callback)
layout = row(select, p)
curdoc().add_root(layout)
# Create a HoverTool: hover
hover = HoverTool(tooltips=[('Country', '@Entity'),
('Production (TWh)', '@TWh'),
('Year', '@Year')], mode='vline')
# Add the hover tool to the figure p
p.add_tools(hover)
output_file('line.html')
show(layout)
目前没有回答
相关问题 更多 >
编程相关推荐