{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version Check\n", "Plotly's python package is updated frequently. Run `pip install plotly --upgrade` to use the latest version." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [ { "data": { "text/plain": [ "'2.4.1'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly\n", "plotly.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Sankey Diagram" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "\n", "data = dict(\n", " type='sankey',\n", " node = dict(\n", " pad = 15,\n", " thickness = 20,\n", " line = dict(\n", " color = \"black\",\n", " width = 0.5\n", " ),\n", " label = [\"A1\", \"A2\", \"B1\", \"B2\", \"C1\", \"C2\"],\n", " color = [\"blue\", \"blue\", \"blue\", \"blue\", \"blue\", \"blue\"]\n", " ),\n", " link = dict(\n", " source = [0,1,0,2,3,3],\n", " target = [2,3,3,4,4,5],\n", " value = [8,4,2,8,4,2]\n", " ))\n", "\n", "layout = dict(\n", " title = \"Basic Sankey Diagram\",\n", " font = dict(\n", " size = 10\n", " )\n", ")\n", "\n", "fig = dict(data=[data], layout=layout)\n", "py.iplot(fig, validate=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create Sankey Canvas" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "import plotly.plotly as py\n", "\n", "data = dict(\n", " type='sankey',\n", " domain = dict(\n", " x = [0,1],\n", " y = [0,1]\n", " ),\n", " orientation = \"h\",\n", " valueformat = \".0f\",\n", " valuesuffix = \"TWh\" \n", " )\n", "\n", "layout = dict(\n", " title = \"Energy forecast for 2050
Source: Department of Energy & Climate Change, Tom Counsell via Mike Bostock\",\n", " font = dict(\n", " size = 10\n", " )\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add Nodes" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [], "source": [ "import plotly.plotly as py\n", "\n", "import urllib, json\n", "\n", "url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'\n", "response = urllib.urlopen(url)\n", "data = json.loads(response.read())\n", "\n", "data_trace = dict(\n", " type='sankey',\n", " domain = dict(\n", " x = [0,1],\n", " y = [0,1]\n", " ),\n", " orientation = \"h\",\n", " valueformat = \".0f\",\n", " valuesuffix = \"TWh\",\n", " node = dict(\n", " pad = 15,\n", " thickness = 15,\n", " line = dict(\n", " color = \"black\",\n", " width = 0.5\n", " ),\n", " label = data['data'][0]['node']['label'],\n", " color = data['data'][0]['node']['color']\n", " )\n", ")\n", "\n", "layout = dict(\n", " title = \"Energy forecast for 2050
Source: Department of Energy & Climate Change, Tom Counsell via Mike Bostock\",\n", " font = dict(\n", " size = 10\n", " )\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add Links" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "inputHidden": false, "outputHidden": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "\n", "import urllib, json\n", "\n", "url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json'\n", "response = urllib.urlopen(url)\n", "data = json.loads(response.read())\n", "\n", "data_trace = dict(\n", " type='sankey',\n", " width = 1118,\n", " height = 772,\n", " domain = dict(\n", " x = [0,1],\n", " y = [0,1]\n", " ),\n", " orientation = \"h\",\n", " valueformat = \".0f\",\n", " valuesuffix = \"TWh\",\n", " node = dict(\n", " pad = 15,\n", " thickness = 15,\n", " line = dict(\n", " color = \"black\",\n", " width = 0.5\n", " ),\n", " label = data['data'][0]['node']['label'],\n", " color = data['data'][0]['node']['color']\n", " ),\n", " link = dict(\n", " source = data['data'][0]['link']['source'],\n", " target = data['data'][0]['link']['target'],\n", " value = data['data'][0]['link']['value'],\n", " label = data['data'][0]['link']['label']\n", " ))\n", "\n", "layout = dict(\n", " title = \"Energy forecast for 2050
Source: Department of Energy & Climate Change, Tom Counsell via Mike Bostock\",\n", " font = dict(\n", " size = 10\n", " )\n", ")\n", "\n", "fig = dict(data=[data_trace], layout=layout)\n", "py.iplot(fig, validate=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Style Sankey Diagram" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "inputHidden": false, "outputHidden": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "\n", "import urllib, json\n", "\n", "url = 'https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy_dark.json'\n", "response = urllib.urlopen(url)\n", "data = json.loads(response.read())\n", "\n", "data_trace = dict(\n", " type='sankey',\n", " width = 1118,\n", " height = 772,\n", " domain = dict(\n", " x = [0,1],\n", " y = [0,1]\n", " ),\n", " orientation = \"h\",\n", " valueformat = \".0f\",\n", " valuesuffix = \"TWh\",\n", " node = dict(\n", " pad = 15,\n", " thickness = 15,\n", " line = dict(\n", " color = \"black\",\n", " width = 0.5\n", " ),\n", " label = data['data'][0]['node']['label']\n", " ),\n", " link = dict(\n", " source = data['data'][0]['link']['source'],\n", " target = data['data'][0]['link']['target'],\n", " value = data['data'][0]['link']['value'],\n", " label = data['data'][0]['link']['label']\n", " ))\n", "\n", "layout = dict(\n", " title = \"Energy forecast for 2050
Source: Department of Energy & Climate Change, Tom Counsell via Mike Bostock\",\n", " font = dict(\n", " size = 10,\n", " color = 'white'\n", " ),\n", " plot_bgcolor = 'black',\n", " paper_bgcolor = 'black'\n", ")\n", "\n", "fig = dict(data=[data_trace], layout=layout)\n", "py.iplot(fig, validate = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reference\n", "\n", "See [https://plotly.com/python/reference/#sankey](https://plotly.com/python/reference/#sankey) for more information and options!" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to c:\\users\\brand\\appdata\\local\\temp\\pip-7q_aoy90-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.11\n", " Uninstalling publisher-0.11:\n", " Successfully uninstalled publisher-0.11\n", " Running setup.py install for publisher: started\n", " Running setup.py install for publisher: finished with status 'done'\n", "Successfully installed publisher-0.11\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n", "\n", "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", "\n", "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n", "\n", "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", "\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'sankey.ipynb', 'python/sankey-diagram/', 'Sankey Diagram',\n", " 'How to make Sankey Diagrams in Python with Plotly.',\n", " title = 'Sankey Diagram | Plotly',\n", " has_thumbnail='true', thumbnail='thumbnail/sankey.jpg', \n", " language='python',\n", " display_as='basic', order=11,\n", " ipynb= '~notebook_demo/151')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernel_info": { "name": "python2" }, "kernelspec": { "display_name": "Python 2", "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.14" } }, "nbformat": 4, "nbformat_minor": 4 }