{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Sustainable Energy Transitions

\n", "
A project by Sgouris Sgouridis and Dénes Csala at Masdar Institute of Science and Technology
\n", "


Plotting notebook for gif social media images

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This workbook will guide you through the mining, processing, formatting and saving the input data for the SET 2.0 model.\n", "\n", "

This is document has been created using IPython Notebook in the Anaconda distribution and it can be edited and run in active mode by clicking download in top right corner of this page. The code is partitioned into sections, called cells. When you are using this workbook in active mode, double-click on a cell to edit it and then run using Ctrl + Enter. Hitting Shift + Enter runs the code and steps into the next cell, while Alt + Enter runs the code and adds a new, empty cell. If you are running this notebook on a presonal computer, you will need a machine with at least 1GB of memory (2GB recommended) and a processor of 1GHz.\n", "

Data

\n", "

The major data sources for this work are the outputs generated by the SET 2.0 model.\n", "

\n", "

Processing

\n", "

The data processing has been done using the Anaconda distribution of Python 2.7 using the IPython Notebook editor. If you have not generated your own data, please download all results files, and run this Iptyhon notebook in the same folder. Then the data is loaded into a pandas dataframe, which represents the backbone of the data analysis. Numerical processing is done with NumPy and for plotting we use matplotlib. Please make sure you have all of these compoents set up in your Python installation. The workbook generates .jpg files to be inlcuded on the main data presentation site.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Code

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import dependencies." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas as pd, numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib as mpl\n", "import matplotlib.font_manager as font_manager\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mycolors=['#dd1c77','#df65b0','#980043']\n", "abc='abcdefghijklmnopqrstuvwxyz' #for plot labeling" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set up save path on your local computer." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "path=\"E:\\Skydrive\\GitHub\\set\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Cross-sections" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "#for plot styling - not necessary\n", "plt.style.use('fivethirtyeight')\n", "#plt.style.use('classic')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fpath = 'Comfortaa.ttf'\n", "prop = font_manager.FontProperties(fname=fpath)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fuels=[u'hydro',u'nuclear',u'biofuels',u'other',u'biomass',u'coal',u'oil',u'gas',u'geoth.',u'wind',u'PV',u'CSP']\n", "#fuelcolors=['#5e4fa2','#3288bd','#66c2a5','#abdda4','#9e0142','#d53e4f','#f46d43','#78c679','#e6f598','#fee08b','#fdae61']\n", "#fuelcolors=['#41b6c4','#1d91c0','#225ea8','#253494','#5e4fa2','#7a0177','#ae017e','#dd3497','#a6d96a','#d9ef8b','#fee08b','#fdae61']\n", "fuelcolors=[np.array([40,40,80])/256.0,\n", " np.array([85,20,52])/256.0,\n", " np.array([131,13,98])/256.0,\n", " np.array([213,9,98])/256.0,\n", " np.array([0,64,16])/256.0,\n", " np.array([112,133,16])/256.0,\n", " np.array([251,212,31])/256.0,\n", " np.array([254,159,28])/256.0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Anaconda\\envs\\py27\\lib\\site-packages\\ipykernel\\__main__.py:5: FutureWarning: pd.rolling_mean is deprecated for DataFrame and will be removed in a future version, replace with \n", "\tDataFrame.rolling(window=9,center=False).mean()\n", "C:\\Anaconda\\envs\\py27\\lib\\site-packages\\ipykernel\\__main__.py:16: FutureWarning: pd.rolling_mean is deprecated for DataFrame and will be removed in a future version, replace with \n", "\tDataFrame.rolling(window=9,center=False).mean()\n" ] } ], "source": [ "#redefine smooth window\n", "moving_window=9\n", "for year in {2015,2020,2025,2030,2035}:\n", " df=pd.read_excel('gif/cut'+str(year)+'.xlsx',sheetname='net')\n", " df=pd.rolling_mean(df,moving_window)\n", " sets={}\n", " for n in df.columns:\n", " sets[n]=[[],[]]\n", " x=df[n].index\n", " m=df[n].as_matrix()\n", " for i in range(len(x)):\n", " sets[n][0].append(x[i])\n", " sets[n][1].append(m[i])\n", "\n", " df=pd.read_excel('gif/cut'+str(year)+'.xlsx',sheetname='gross')\n", " df=pd.rolling_mean(df,moving_window)\n", " sets2={}\n", " for n in df.columns:\n", " sets2[n]=[[],[]]\n", " x=df[n].index\n", " m=df[n].as_matrix()\n", " for i in range(len(x)):\n", " sets2[n][0].append(x[i])\n", " sets2[n][1].append(m[i])\n", " sets2.pop('net');\n", "\n", " x=sets[u'CSP'][0]\n", " y=[0 for i in range(len(x))]\n", " net=pd.DataFrame([0 for i in range(len(x))])\n", " gross=pd.DataFrame([0 for i in range(len(x))])\n", " labels=[]\n", "\n", " fig = plt.figure(figsize=(8,5)) \n", " ax=[]\n", " ax.append(plt.subplot2grid((2,2), (0,0), rowspan=2, colspan=2, axisbg=None, axisbelow=True))\n", " fig.tight_layout() \n", " fig.subplots_adjust(wspace=0.35)\n", "\n", " for i in range(len(fuels[5:])):\n", " fuel=fuels[::-1][i]\n", " if fuel==u'geoth.':fuel='geo'\n", " gross+=pd.DataFrame(sets2[fuel][1])\n", " y=np.vstack((sets2[fuel][1],y)) \n", "\n", " g=pd.DataFrame(range(151))\n", " g[0]=0\n", " for i in range(len(fuels)):\n", " fuel=fuels[::-1][i]\n", " gross+=pd.DataFrame(sets[fuel][1])\n", " net+=pd.DataFrame(sets[fuel][1])\n", " if fuel not in {u'hydro',u'nuclear',u'biofuels',u'other',u'biomass'}:\n", " y=np.vstack((sets[fuel][1],y)) \n", " else:\n", " g+=pd.DataFrame(sets[fuel][1])\n", " y=np.vstack((g[0],y))\n", "\n", " sp=ax[0].stackplot(x,y,alpha=0.9,colors=fuelcolors+fuelcolors[1:])\n", " proxy = [mpl.patches.Rectangle((0,0), 0,0, facecolor=(fuelcolors+fuelcolors[1:])[j-1]) for j,pol in enumerate(sp)][::-1]\n", " #ax[0].legend(proxy[7:], (labels[7:]),loc=2,framealpha=0,fontsize=11) \n", "\n", " ax2=ax[0].twinx()\n", " ax3=ax[0].twinx()\n", " ax2.plot([0],[0],linewidth=2,linestyle='-',color='k',label='Net')\n", " ax3.plot([0],[0],linewidth=2,linestyle='--',color='w',label=' ')\n", " ax2.plot(x,net,linewidth=2,linestyle='--',color='w')\n", " ax2.plot([1958,2100],[2000,2001],lw=0.5,color=\"grey\")\n", "\n", " ax[0].set_ylim(0,260000)\n", " ax2.set_ylim(0,260000)\n", " ax[0].set_xlim(1950,2100)\n", " ax[0].set_yticklabels([0,50,100,150,200,250],fontproperties=prop,size=12)\n", " ax[0].set_xticklabels([1940,1960,1980,2000,2020,2040,2060,2080,2100],fontproperties=prop,size=12)\n", " #ax[0].set_xlabel('Year',size=12)\n", " ax[0].set_ylabel('Primary Energy Demand [1000 TWh / year]',fontproperties=prop,size=14,labelpad=10)\n", " ax2.set_yticklabels([])\n", " ax3.set_yticklabels([])\n", " ax3.set_yticks([])\n", " ax2.legend(ncol=2,loc=2,framealpha=0,fontsize=12)\n", " ax3.legend(ncol=2,loc=2,framealpha=0,fontsize=12)\n", " ax2.text(1.045, 0.79, '2000 W',\n", " horizontalalignment='right',fontproperties=prop,\n", " verticalalignment='center',rotation=90,\n", " transform=ax2.transAxes,size=14,color='k')\n", "\n", " ax[0].text(0.5, 1.1, r\"Fossil phase-out start: \"+str(year)+\" | Emissions cap: 990 GtCO$_2$\",\n", " horizontalalignment='center',\n", " verticalalignment='bottom',fontproperties=prop,\n", " transform=ax[0].transAxes,size=14,color='k')\n", " ax[0].text(0.5, 1.03, r\"EROEI = 20 in year 2014 | Demand = 2000 W / person in year 2100\",\n", " horizontalalignment='center',\n", " verticalalignment='bottom',fontproperties=prop,\n", " transform=ax[0].transAxes,size=14,color='k')\n", "\n", " ax[0].plot([2014,2014.001],[160000,250000],color='grey',ls='--',lw=1)\n", " ax[0].text(0.43, 0.96, r\"historical projection\",\n", " horizontalalignment='center',\n", " verticalalignment='top',fontproperties=prop,\n", " transform=ax[0].transAxes,size=10,color='grey')\n", "\n", " ax[0].grid('off')\n", " ax2.grid('off')\n", " ax3.grid('off')\n", " \n", " plt.suptitle('Detailed Sustainable Energy Transition Path',fontproperties=prop,size=17,y=1.15)\n", " plt.savefig(path+'/gif/twittercut'+str(year)+'.png',bbox_inches = 'tight', pad_inches = 0.1, dpi=150)\n", " plt.close()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Anaconda\\envs\\py27\\lib\\site-packages\\ipykernel\\__main__.py:5: FutureWarning: pd.rolling_mean is deprecated for DataFrame and will be removed in a future version, replace with \n", "\tDataFrame.rolling(window=9,center=False).mean()\n", "C:\\Anaconda\\envs\\py27\\lib\\site-packages\\ipykernel\\__main__.py:16: FutureWarning: pd.rolling_mean is deprecated for DataFrame and will be removed in a future version, replace with \n", "\tDataFrame.rolling(window=9,center=False).mean()\n" ] } ], "source": [ "#redefine smooth window\n", "moving_window=9\n", "for year in {500,750,1000,1250,1500}:\n", " df=pd.read_excel('gif/cut'+str(year)+'.xlsx',sheetname='net')\n", " df=pd.rolling_mean(df,moving_window)\n", " sets={}\n", " for n in df.columns:\n", " sets[n]=[[],[]]\n", " x=df[n].index\n", " m=df[n].as_matrix()\n", " for i in range(len(x)):\n", " sets[n][0].append(x[i])\n", " sets[n][1].append(m[i])\n", "\n", " df=pd.read_excel('gif/cut'+str(year)+'.xlsx',sheetname='gross')\n", " df=pd.rolling_mean(df,moving_window)\n", " sets2={}\n", " for n in df.columns:\n", " sets2[n]=[[],[]]\n", " x=df[n].index\n", " m=df[n].as_matrix()\n", " for i in range(len(x)):\n", " sets2[n][0].append(x[i])\n", " sets2[n][1].append(m[i])\n", " sets2.pop('net');\n", "\n", " x=sets[u'CSP'][0]\n", " y=[0 for i in range(len(x))]\n", " net=pd.DataFrame([0 for i in range(len(x))])\n", " gross=pd.DataFrame([0 for i in range(len(x))])\n", " labels=[]\n", "\n", " fig = plt.figure(figsize=(8,5)) \n", " ax=[]\n", " ax.append(plt.subplot2grid((2,2), (0,0), rowspan=2, colspan=2, axisbg=None, axisbelow=True))\n", " fig.tight_layout() \n", " fig.subplots_adjust(wspace=0.35)\n", "\n", " for i in range(len(fuels[5:])):\n", " fuel=fuels[::-1][i]\n", " if fuel==u'geoth.':fuel='geo'\n", " gross+=pd.DataFrame(sets2[fuel][1])\n", " y=np.vstack((sets2[fuel][1],y)) \n", "\n", " g=pd.DataFrame(range(151))\n", " g[0]=0\n", " for i in range(len(fuels)):\n", " fuel=fuels[::-1][i]\n", " gross+=pd.DataFrame(sets[fuel][1])\n", " net+=pd.DataFrame(sets[fuel][1])\n", " if fuel not in {u'hydro',u'nuclear',u'biofuels',u'other',u'biomass'}:\n", " y=np.vstack((sets[fuel][1],y)) \n", " else:\n", " g+=pd.DataFrame(sets[fuel][1])\n", " y=np.vstack((g[0],y))\n", "\n", " sp=ax[0].stackplot(x,y,alpha=0.9,colors=fuelcolors+fuelcolors[1:])\n", " proxy = [mpl.patches.Rectangle((0,0), 0,0, facecolor=(fuelcolors+fuelcolors[1:])[j-1]) for j,pol in enumerate(sp)][::-1]\n", " #ax[0].legend(proxy[7:], (labels[7:]),loc=2,framealpha=0,fontsize=11) \n", "\n", " ax2=ax[0].twinx()\n", " ax3=ax[0].twinx()\n", " ax2.plot([0],[0],linewidth=2,linestyle='-',color='k',label='Net')\n", " ax3.plot([0],[0],linewidth=2,linestyle='--',color='w',label=' ')\n", " ax2.plot(x,net,linewidth=2,linestyle='--',color='w')\n", " ax2.plot([1958,2100],[2000,2001],lw=0.5,color=\"grey\")\n", "\n", " ax[0].set_ylim(0,260000)\n", " ax2.set_ylim(0,260000)\n", " ax[0].set_xlim(1950,2100)\n", " ax[0].set_yticklabels([0,50,100,150,200,250],fontproperties=prop,size=12)\n", " ax[0].set_xticklabels([1940,1960,1980,2000,2020,2040,2060,2080,2100],fontproperties=prop,size=12)\n", " #ax[0].set_xlabel('Year',size=12)\n", " ax[0].set_ylabel('Primary Energy Demand [1000 TWh / year]',fontproperties=prop,size=14,labelpad=10)\n", " ax2.set_yticklabels([])\n", " ax3.set_yticklabels([])\n", " ax3.set_yticks([])\n", " ax2.legend(ncol=2,loc=2,framealpha=0,fontsize=12)\n", " ax3.legend(ncol=2,loc=2,framealpha=0,fontsize=12)\n", " ax2.text(1.045, 0.79, '2000 W',\n", " horizontalalignment='right',fontproperties=prop,\n", " verticalalignment='center',rotation=90,\n", " transform=ax2.transAxes,size=14,color='k')\n", "\n", " ax[0].text(0.5, 1.1, r\"Fossil phase-out start: \"+str(year)+\" | Emissions cap: 990 GtCO$_2$\",\n", " horizontalalignment='center',\n", " verticalalignment='bottom',fontproperties=prop,\n", " transform=ax[0].transAxes,size=14,color='k')\n", " ax[0].text(0.5, 1.03, r\"EROEI = 20 in year 2014 | Demand = 2000 W / person in year 2100\",\n", " horizontalalignment='center',\n", " verticalalignment='bottom',fontproperties=prop,\n", " transform=ax[0].transAxes,size=14,color='k')\n", "\n", " ax[0].plot([2014,2014.001],[160000,250000],color='grey',ls='--',lw=1)\n", " ax[0].text(0.43, 0.96, r\"historical projection\",\n", " horizontalalignment='center',\n", " verticalalignment='top',fontproperties=prop,\n", " transform=ax[0].transAxes,size=10,color='grey')\n", "\n", " ax[0].grid('off')\n", " ax2.grid('off')\n", " ax3.grid('off')\n", " \n", " plt.suptitle('Detailed Sustainable Energy Transition Path',fontproperties=prop,size=17,y=1.15)\n", " plt.savefig(path+'/gif/twittercut'+str(year)+'.png',bbox_inches = 'tight', pad_inches = 0.1, dpi=150)\n", " plt.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "


We would like to express our gratitude to all of the developers of the libraries used and especially to the affiliates of EIA, BP and World Bank for their great database and openly accesible data. The data manipulation algorithms are open sourced and freely reproducible under an MIT license.

\n", "
\n", "

Dénes Csala  |  2016

" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }