National generation capacity: Check notebook
This Notebook is part of the National Generation Capacity Datapackage of Open Power System Data. |
The notebook extends the processing notebook to make visualisations.
In [ ]:
import os.path
import math
import functions.plots as fp # predefined functions in extra file
import bokeh.plotting as plo
from bokeh.io import show, output_notebook
from bokeh.layouts import row, column
from bokeh.models import Panel, Tabs
from bokeh.models.widgets import RangeSlider, MultiSelect, Select
output_notebook()
In [ ]:
data_file = 'national_generation_capacity_stacked.csv'
filepath = os.path.join('output', data_file)
data = fp.load_opsd_data(filepath)
data.head()
Select individual width and height that fits your jupyter notebook settings.
In [ ]:
width = 1000
height = 500
In [ ]:
def comparison_plot(doc):
# init of 5 plots for each energy level
sources = []
plots = []
for level in fp.energy_levels:
# init plots with the predfined function
s, p = fp.init_plot(data, level, size=(width, height))
sources.append(s)
plots.append(p)
# associate each plot with a tab of the interactive plot
panels= []
for p, level in zip(plots, fp.energy_levels):
panels.append(Panel(child=p, title=level))
tabs = Tabs(tabs=panels, tabs_location='below', active=2)
# Range slider for available years
oldest_year = min(data["year"])
newest_year = max(data["year"])
y_slider = RangeSlider(title="Years",
value=(2015,2016),
start=oldest_year,
end=newest_year,
step=1)
# Select field for sources
m_select = MultiSelect(title="Available Sources:",
value=fp.global_sources,
options=[(s,s) for s in fp.global_sources])
# Select button for countries
countries = list(data["country"].unique())
c_select = Select(title="Country", options=countries, value='FR')
# catch all widgets
wid = [c_select, y_slider, m_select]
rows = row(wid)
# update function for `on_change` trigger
def update(attrname, old, new):
y = y_slider.value
y_range = [x for x in range(y[0],y[1]+1)]
s_selected = m_select.value
co = c_select.value
# run update for each plot
for p, s, l in zip(plots, sources, fp.energy_levels):
df = fp.filter_data_set(data, co, y_range, s_selected, l)
source_data, x_axis = fp.prepare_data(df)
s.data = source_data
p.x_range.factors = x_axis
# associate `update` function with each widget to apply updates for each change
for w in wid:
w.on_change('value', update)
layout = column(rows, tabs)
doc.add_root(layout)
After the bokeh plot is set up a bokeh server is started to make the plot interactive.
Possible options:
- Select a country from the dropdown menu in the top left
- Select a range of years from the range slider
- Multiselect the sources you want to compare in the top right
- Choose which "energy level" you want to investigate with the tabs below the plot
In [ ]:
show(comparison_plot)
In [ ]: