{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# US energy consumption\n", "\n", "This example is based on the [Sankey diagrams of US energy consumption from the Lawrence Livermore National Laboratory](https://flowcharts.llnl.gov/) (thanks to John Muth for the suggestion and transcribing the data). We jump straight to the final result – for more explanation of the steps and concepts, see the [tutorials](../tutorials/index.ipynb)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from floweaver import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the dataset:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset = Dataset.from_csv('us-energy-consumption.csv',\n", " dim_process_filename='us-energy-consumption-processes.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This defines the order the nodes appear in:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sources = ['Solar', 'Nuclear', 'Hydro', 'Wind', 'Geothermal',\n", " 'Natural_Gas', 'Coal', 'Biomass', 'Petroleum']\n", "\n", "uses = ['Residential', 'Commercial', 'Industrial', 'Transportation']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now define the Sankey diagram definition." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nodes = {\n", " 'sources': ProcessGroup('type == \"source\"', Partition.Simple('process', sources), title='Sources'),\n", " 'imports': ProcessGroup(['Net_Electricity_Import'], title='Net electricity imports'),\n", " 'electricity': ProcessGroup(['Electricity_Generation'], title='Electricity Generation'),\n", " 'uses': ProcessGroup('type == \"use\"', partition=Partition.Simple('process', uses)),\n", " \n", " 'energy_services': ProcessGroup(['Energy_Services'], title='Energy services'),\n", " 'rejected': ProcessGroup(['Rejected_Energy'], title='Rejected energy'),\n", " \n", " 'direct_use': Waypoint(Partition.Simple('source', [\n", " # This is a hack to hide the labels of the partition, there should be a better way...\n", " (' '*i, [k]) for i, k in enumerate(sources)\n", " ])),\n", "}\n", "\n", "ordering = [\n", " [[], ['sources'], []],\n", " [['imports'], ['electricity', 'direct_use'], []],\n", " [[], ['uses'], []],\n", " [[], ['rejected', 'energy_services'], []]\n", "]\n", "\n", "bundles = [\n", " Bundle('sources', 'electricity'),\n", " Bundle('sources', 'uses', waypoints=['direct_use']),\n", " Bundle('electricity', 'uses'),\n", " Bundle('imports', 'uses'),\n", " Bundle('uses', 'energy_services'),\n", " Bundle('uses', 'rejected'),\n", " Bundle('electricity', 'rejected'),\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the colours to roughly imitate the original Sankey diagram:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "palette = {\n", " 'Solar': 'gold',\n", " 'Nuclear': 'red',\n", " 'Hydro': 'blue',\n", " 'Wind': 'purple',\n", " 'Geothermal': 'brown',\n", " 'Natural_Gas': 'steelblue',\n", " 'Coal': 'black',\n", " 'Biomass': 'lightgreen',\n", " 'Petroleum': 'green',\n", " 'Electricity': 'orange',\n", " 'Rejected energy': 'lightgrey',\n", " 'Energy services': 'dimgrey',\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here's the result!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sdd = SankeyDefinition(nodes, bundles, ordering,\n", " flow_partition=dataset.partition('type'))\n", "weave(sdd, dataset, palette=palette) \\\n", " .to_widget(width=700, height=450, margins=dict(left=100, right=120), debugging=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }