diff --git a/TEMBA_results_binder test.ipynb b/TEMBA_results_binder test.ipynb index fd85e17..75a489a 100644 --- a/TEMBA_results_binder test.ipynb +++ b/TEMBA_results_binder test.ipynb @@ -1,151 +1,20 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "extensions": { - "jupyter_dashboards": { - "version": 1, - "views": { - "grid_default": {}, - "report_default": {} - } - } - }, - "scrolled": false - }, - "outputs": [ - { - "data": { - "text/html": [ - "" - ], - "text/vnd.plotly.v1+html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "" - ], - "text/vnd.plotly.v1+html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'coalbiomasschart'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0mcufflinks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgo_offline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[0mcufflinks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_config_file\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mworld_readable\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtheme\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'white'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mcoalbiomasschart\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcoal_biomass_chart\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 22\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mcrudechart\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcrude_chart\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0memissionschart\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0memissions_chart\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'coalbiomasschart'" - ] - } - ], - "source": [ - "import pandas as pd\n", - "import requests\n", - "import csv\n", - "import pickle\n", - "import os, sys\n", - "import numpy as np\n", - "from IPython.display import HTML\n", - "import ipywidgets as widgets\n", - "from IPython.display import display\n", - "import matplotlib as plt\n", - "from ipywidgets import interact, interactive, fixed, interact_manual\n", - "#importing plotly and cufflinks in offline mode\n", - "import plotly as py\n", - "import plotly.graph_objs as go\n", - "import cufflinks\n", - "import plotly.offline as pyo\n", - "from plotly.offline import plot, iplot, init_notebook_mode\n", - "pyo.init_notebook_mode()\n", - "cufflinks.go_offline()\n", - "cufflinks.set_config_file(world_readable=True, theme='white')\n", - "from coalbiomasschart import coal_biomass_chart\n", - "from crudechart import crude_chart\n", - "from emissionschart import emissions_chart\n", - "from gaschart import gas_chart\n", - "from hfolfochart import hfo_lfo_chart\n", - "from powerchart import power_chart\n", - "from waterchart import water_chart\n", - "#import tkinter for prompting the user to choose a particular file\n", - "#from tkinter import filedialog\n", - "#from tkinter import *\n", - "#print(py.__version__)\n", - "#print(cufflinks.__version__)\n", - "print('TEMBA visualization: Original code written by \\033[95mAbhishek Shivakumar\\033[0m.\\n\\t\\t Adapted by\\033[95m Vignesh Sridharan\\033[0m.')\n" - ] - }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Choice of power pools\n", - "pools=['EAPP','NAPP','SAPP','CAPP','WAPP']" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# Function to choose the corresponding pickle file absed on the selected power pool\n", - "def result_select(Power_pools):\n", - " if Power_pools=='NAPP':\n", - " picklefile='./napp.pickle'\n", - " elif Power_pools=='EAPP':\n", - " picklefile='./eapp.pickle'\n", - " elif Power_pools=='SAPP':\n", - " picklefile='./sapp.pickle'\n", - " elif Power_pools=='WAPP':\n", - " picklefile='./wapp.pickle' \n", - " elif Power_pools=='CAPP':\n", - " picklefile='./capp.pickle'\n", - " \n", - " return picklefile" + "from visualise_temba import powerpool_selector\n", + "from chart import power_chart, water_chart\n", + "from IPython.display import display\n", + "from ipywidgets import interact, interactive, fixed\n", + "import pandas as pd\n", + "\n", + "country_code = pd.read_csv('./countrycode.csv', sep=',')\n", + "agg_pow_col = pd.read_csv('./agg_pow_col.csv', sep=',').to_dict('list')\n", + "t_include = list(pd.read_csv('./power_tech.csv', sep=',')['power_tech'])" ] }, { @@ -159,169 +28,14 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "e83e964bcfca4b4fbb1da9adcc5e57d3", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Power_pools', options=('EAPP', 'NAPP', 'SAPP', 'CAPP', 'WAPP'), va…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# interactive widget for choosing the power pools\n", - "Powerpool_Selector = interactive(result_select,Power_pools=pools)\n", - "display(Powerpool_Selector)" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [], - "source": [ - "# The pickle file for the corressponding powerpool in unbundled in the follwoing steps\n", - "picklefile=Powerpool_Selector.result\n", - "pkl_file = open(picklefile, 'rb')\n", - "# The pickle file is loaded onto the all_params dictionary\n", - "all_params = pickle.load(pkl_file)" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "extensions": { - "jupyter_dashboards": { - "version": 1, - "views": { - "grid_default": {}, - "report_default": {} - } - } - }, - "scrolled": true - }, - "outputs": [], - "source": [ - "#Fundamental dictionaries that govern naming and colour coding\n", - "url1='https://raw.githubusercontent.com/vignesh1987/ipywidgets/master/agg_col.csv'\n", - "url2='https://raw.githubusercontent.com/vignesh1987/ipywidgets/master/agg_pow_col.csv'\n", - "url3='https://raw.githubusercontent.com/vignesh1987/ipywidgets/master/countrycode.csv'\n", - "url4='https://raw.githubusercontent.com/vignesh1987/ipywidgets/master/power_tech.csv'\n", - "url5='https://raw.githubusercontent.com/vignesh1987/ipywidgets/master/techcodes.csv'\n", - "colorcode=pd.read_csv(url5,sep=',')\n", - "colorcode1=colorcode.drop('colour',axis=1)\n", - "colorcode2=colorcode.drop('tech_code',axis=1)\n", - "det_col=dict([(a,b) for a,b in zip(colorcode1.tech_code,colorcode1.tech_name)])\n", - "color_dict=dict([(a,b) for a,b in zip(colorcode2.tech_name,colorcode2.colour)])\n", - "agg1=pd.read_csv(url1,sep=',')\n", - "agg2=pd.read_csv(url2,sep=',')\n", - "agg_col=agg1.to_dict('list')\n", - "agg_pow_col=agg2.to_dict('list')\n", - "power_tech=pd.read_csv(url4,sep=',')\n", - "t_include = list(power_tech['power_tech'])\n", - "#Country code list\n", - "country_code=pd.read_csv(url3,sep=',')" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "extensions": { - "jupyter_dashboards": { - "version": 1, - "views": { - "grid_default": {}, - "report_default": {} - } - } - }, - "scrolled": true - }, - "outputs": [], - "source": [ - "# time period definition\n", - "years = pd.Series(range(2015,2071))" - ] - }, - { - "cell_type": "code", - "execution_count": 37, + "execution_count": null, "metadata": { - "extensions": { - "jupyter_dashboards": { - "version": 1, - "views": { - "grid_default": {}, - "report_default": {} - } - } - }, "scrolled": true }, "outputs": [], "source": [ - "#base function used for many different variables (mainly cost)\n", - "def df_filter(df,lb,ub,t_exclude):\n", - " df['t'] = df['t'].str[lb:ub]\n", - " df['value'] = df['value'].astype('float64')\n", - " df = df[~df['t'].isin(t_exclude)].pivot_table(index='y', \n", - " columns='t',\n", - " values='value', \n", - " aggfunc='sum').reset_index().fillna(0)\n", - " df = df.reindex(sorted(df.columns), axis=1).set_index('y').reset_index().rename(columns=det_col)\n", - " df['y'] = years\n", - " df=df[df['y']>2018]\n", - " return df\n", - "#### PLotting function for all graphs except Gas (as it needs relative charts)\n", - "def df_plot(df,y_title,p_title):\n", - " if len(df.columns)==1:\n", - " print('There are no values for the result variable that you want to plot')\n", - " else:\n", - " return df.iplot(x='y',\n", - " kind='bar', \n", - " barmode='stack',\n", - " xTitle='Year',\n", - " yTitle=y_title,\n", - " color=[color_dict[x] for x in df.columns if x != 'y'],\n", - " title=p_title,showlegend=True\n", - " )\n", - "#### Emissions#####\n", - "def df_filter_emission_tech(df,lb,ub):\n", - " df['t'] = df['t'].str[lb:ub]\n", - " df['e'] = df['e'].str[2:5]\n", - " df['value'] = df['value'].astype('float64')\n", - " df = df.pivot_table(index='y',columns='t',\n", - " values='value',\n", - " aggfunc='sum').reset_index().fillna(0)\n", - " df = df.reindex(sorted(df.columns), axis=1).set_index('y').reset_index().rename(columns=det_col)\n", - " df['y'] = years\n", - " df=df[df['y']>2018]\n", - " return df\n", - "### Annual Emissions\n", - "def df_filter_emission_tot(df):\n", - " df['e'] = df['e'].str[2:5]\n", - " df['value'] = df['value'].astype('float64')\n", - " df = df.pivot_table(index='y',columns='e',\n", - " values='value',\n", - " aggfunc='sum').reset_index().fillna(0)\n", - " df = df.reindex(sorted(df.columns), axis=1).set_index('y').reset_index().rename(columns=det_col)\n", - " df['y'] = years\n", - " df=df[df['y']>2018]\n", - " return df\n" + "p = powerpool_selector()\n", + "display(p)" ] }, { @@ -333,7 +47,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": { "extensions": { "jupyter_dashboards": { @@ -346,24 +60,11 @@ }, "scrolled": false }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "dd402d7db09e49819e9d1288fc46f149", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ - "Power_graphs = interact(power_chart, Country=country_code['Country Name'])" + "Power_graphs = interactive(power_chart, Country=country_code['Country Name'], \n", + " all_params=fixed(p.result), agg_pow_col=fixed(agg_pow_col), t_include=fixed(t_include))\n", + "display(Power_graphs)" ] }, { @@ -375,26 +76,11 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "2d46a1e762a3494bb2df23bcd5a8038e", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ - "Water_graphs = interact(water_chart, Country=country_code['Country Name'])" + "Water_graphs = interact(water_chart, Country=country_code['Country Name'], all_params=fixed(p.result), t_include=fixed(t_include))" ] }, { @@ -406,26 +92,11 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": { "scrolled": false }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "025b00ef1358455192fd9a1dfebb3f36", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "Emission_graphs = interact(emissions_chart, Country=country_code['Country Name'])" ] @@ -439,24 +110,9 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "7c7d7c4493f34ddfaea6b8867267f2b8", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "Gas_graphs = interact(gas_chart, Country=country_code['Country Name'])" ] @@ -470,24 +126,9 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "db5d9043dd4449319eb76c7c9ebed4fa", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "Crude_Oil_graphs= interact(crude_chart, Country=country_code['Country Name'])" ] @@ -501,24 +142,9 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "3073b3a1a4ee434ba4c3c56e45e00749", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "coal_biom_graphs = interact(coal_biomass_chart, Country=country_code['Country Name'])" ] @@ -532,24 +158,9 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "3fc112ca422242f790280e86a5bc4cd2", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(Dropdown(description='Country', options=('Algeria', 'Egypt', 'Libya', 'Mauritania', 'Mor…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "hfo_lfo_graphs = interact(hfo_lfo_chart, Country=country_code['Country Name'])\n" ] @@ -597,7 +208,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/chart.py b/chart.py new file mode 100644 index 0000000..488a442 --- /dev/null +++ b/chart.py @@ -0,0 +1,177 @@ +from plot import df_plot +import pandas as pd +from functools import lru_cache + +def get_colour_code(): + + colorcode = pd.read_csv('./techcodes.csv', sep=',') + colorcode1 = colorcode.drop('colour', axis=1) + return dict([(a,b) for a,b in zip(colorcode1.tech_code, colorcode1.tech_name)]) + +@lru_cache(maxsize=64) +def get_country_code(Country): + + country_code = pd.read_csv('./countrycode.csv', sep=',') + return country_code[country_code['Country Name'] == Country]['Country code'].tolist()[0] + +def water_chart(Country, all_params, t_include): + """Plots water abstraction results + """ + cc = get_country_code(Country) + det_col = get_colour_code() + agg_col = pd.read_csv('./agg_col.csv', sep=',').to_dict('list') + + # water withdrawal detailed + wat_w_df = all_params['UseByTechnologyAnnual'] + wat_w_df = wat_w_df[wat_w_df['f'].str[:6] == cc + 'WAT1'].copy() + + wat_w_df['t'] = wat_w_df['t'].str[2:10] + wat_w_df['value'] = wat_w_df['value'].astype('float64') + wat_w_df = wat_w_df.pivot_table(index='y', + columns='t', + values='value', + aggfunc='sum').reset_index().fillna(0) + wat_w_df = wat_w_df.reindex(sorted(wat_w_df.columns), axis=1).set_index('y').reset_index().rename(columns=det_col) + # Water Withdrawal (Aggregated) + watw_agg_df = pd.DataFrame(columns=agg_col) + watw_agg_df.insert(0,'y',wat_w_df['y']) + watw_agg_df = watw_agg_df.fillna(0.00) + for each in agg_col: + for tech_exists in agg_col[each]: + if tech_exists in wat_w_df.columns: + watw_agg_df[each] = watw_agg_df[each] + wat_w_df[tech_exists] + watw_agg_df[each] = watw_agg_df[each].round(2) + + df_plot(watw_agg_df, 'Million cubic metres (Mm^3)', cc + "-" + 'Water Withdrawal') + + # water output detailed + wat_o_df = all_params['ProductionByTechnologyAnnual'] + wat_o_df = wat_o_df[wat_o_df['f'].str[:6]==cc+'WAT2'].copy() + wat_o_df['t'] = wat_o_df['t'].str[2:10].copy() + wat_o_df['value'] = wat_o_df['value'].astype('float64') + wat_o_df = wat_o_df.pivot_table(index='y', + columns='t', + values='value', + aggfunc='sum').reset_index().fillna(0) + wat_o_df = wat_o_df.reindex(sorted(wat_o_df.columns), axis=1).set_index('y').reset_index().rename(columns=det_col) + + #Water consumption missing row additions + for wd in wat_w_df.columns: + for wc in wat_o_df.columns: + if wd in wat_o_df.columns: + pass + else: + wat_o_df[wd] = 0 + ##### + ####Water consumption (Detailed) + wat_c_df = wat_w_df.set_index('y') - wat_o_df.set_index('y') + wat_c_df = wat_c_df.fillna(0.00) + wat_c_df.reset_index(inplace=True) + # Water consumption (Aggregate) + watc_agg_df = pd.DataFrame(columns=agg_col) + watc_agg_df.insert(0,'y',wat_c_df['y']) + watc_agg_df = watc_agg_df.fillna(0.00) + for each in agg_col: + for tech_exists in agg_col[each]: + if tech_exists in wat_c_df.columns: + watc_agg_df[each] = watc_agg_df[each] + wat_c_df[tech_exists] + watc_agg_df[each] = watc_agg_df[each].round(2) + df_plot(watc_agg_df, 'Million cubic metres (Mm^3)',cc+'-'+'Water consumption aggregated') + + +def power_chart(Country, all_params, agg_pow_col, t_include): + + cc = get_country_code(Country) + det_col = get_colour_code() + + # colorcode = pd.read_csv('./techcodes.csv', sep=',') + # colorcode2 = colorcode.drop('tech_code', axis=1) + # color_dict = dict([(a,b) for a,b in zip(colorcode2.tech_name, colorcode2.colour)]) + + # Power capacity (detailed) + cap_df = all_params['TotalCapacityAnnual'] + cap_df = cap_df[cap_df['t'].str[:2]==cc].copy() + cap_df['t'] = cap_df['t'].str[2:10] + cap_df['value'] = cap_df['value'].astype('float64') + cap_df = cap_df[cap_df['t'].isin(t_include)].pivot_table(index='y', + columns='t', + values='value', + aggfunc='sum').reset_index().fillna(0) + cap_df = cap_df.reindex(sorted(cap_df.columns), axis=1 + ).set_index('y' + ).reset_index( + ).rename(columns=det_col) + #*********************************************** + # Power capacity (Aggregated) + cap_agg_df = pd.DataFrame(columns=agg_pow_col) + cap_agg_df.insert(0,'y',cap_df['y']) + cap_agg_df = cap_agg_df.fillna(0.00) + # + for each in agg_pow_col: + for tech_exists in agg_pow_col[each]: + if tech_exists in cap_df.columns: + cap_agg_df[each] = cap_agg_df[each] + cap_df[tech_exists] + cap_agg_df[each] = cap_agg_df[each].round(3) + # + df_plot(cap_agg_df, 'Gigawatts (GW)', + cc + "-" + 'Power Generation Capacity (Aggregate)') + ## Power generation (Detailed) + gen_df = all_params['ProductionByTechnologyAnnual'].copy() + gen_df = gen_df[(gen_df['f'].str[:2] == cc)].copy() + gen_df = gen_df[(gen_df['f'].str[2:6] == 'EL01') | (gen_df['f'].str[2:6] == 'EL03')].copy() + gen_df = gen_df[(gen_df['t'].str[2:10] != 'EL00T00X') & (gen_df['t'].str[2:10] != 'EL00TDTX')].copy() + gen_df['value'] = gen_df['value'].astype('float64') + gen_df = gen_df.pivot_table(index='y', + columns='t', + values='value', + aggfunc='sum').reset_index().fillna(0) + for each in gen_df.columns: + if len(each)!=1: + if (each[2:4]=='EL') & (each[6:10]=='BP00'): + pass + else: + gen_df.rename(columns={each:each[2:10]},inplace=True) + else: + pass + gen_df = gen_df.reindex(sorted(gen_df.columns), axis=1 + ).set_index('y' + ).reset_index( + ).rename(columns=det_col) + ##### + # Power generation (Aggregated) + gen_agg_df = pd.DataFrame(columns=agg_pow_col) + gen_agg_df.insert(0,'y',gen_df['y']) + gen_agg_df = gen_agg_df.fillna(0.00) + for each in agg_pow_col: + for tech_exists in agg_pow_col[each]: + if tech_exists in gen_df.columns: + gen_agg_df[each] = gen_agg_df[each] + gen_df[tech_exists] + gen_agg_df[each] = gen_agg_df[each].round(2) + df_plot(gen_agg_df, 'Petajoules (PJ)', cc+"-"+'Power Generation (Aggregate)') + # New capacity (detailed) + cap_new_df = all_params['NewCapacity'] + cap_new_df = cap_new_df[cap_new_df['t'].str[:2]==cc].copy() + cap_new_df['t'] = cap_new_df['t'].str[2:10] + cap_new_df['value'] = cap_new_df['value'].astype('float64') + cap_new_df = cap_new_df[cap_new_df['t'].isin(t_include)].pivot_table( + index='y', columns='t', values='value', aggfunc='sum' + ).reset_index().fillna(0) + cap_new_df = cap_new_df.reindex(sorted(cap_new_df.columns), axis=1 + ).set_index('y' + ).reset_index( + + ).rename(columns=det_col) + #*********************************************** + # Power capacity (Aggregated) + cap_new_agg_df = pd.DataFrame(columns=agg_pow_col) + cap_new_agg_df.insert(0, 'y', cap_new_df['y']) + cap_new_agg_df = cap_new_agg_df.fillna(0.00) + # + for each in agg_pow_col: + for tech_exists in agg_pow_col[each]: + if tech_exists in cap_new_df.columns: + cap_new_agg_df[each] = cap_new_agg_df[each] + cap_new_df[tech_exists] + cap_new_agg_df[each] = cap_new_agg_df[each].round(3) + ## + df_plot(cap_new_agg_df, 'Gigawatts (GW)', + cc + "-" + 'New power generation capacity (Aggregate)') diff --git a/plot.py b/plot.py new file mode 100644 index 0000000..bad8f98 --- /dev/null +++ b/plot.py @@ -0,0 +1,20 @@ +import pandas as pd + +def df_plot(df, y_title, p_title): + + colorcode = pd.read_csv('./techcodes.csv', sep=',') + colorcode2 = colorcode.drop('tech_code', axis=1) + + color_dict = dict([(a,b) for a,b in zip(colorcode2.tech_name, colorcode2.colour)]) + + if len(df.columns) == 1: + print('There are no values for the result variable that you want to plot') + else: + return df.iplot(x='y', + kind='bar', + barmode='stack', + xTitle='Year', + yTitle=y_title, + color=[color_dict[x] for x in df.columns if x != 'y'], + title=p_title, + showlegend=True)