{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Sankey diagram updates\n", "Version 3.7.0 includes several important updates to the Sankey trace type" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.7.0\n" ] }, { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import plotly\n", "print(plotly.__version__)\n", "\n", "import plotly.graph_objs as go\n", "from plotly.offline import init_notebook_mode, iplot\n", "from plotly.tools import make_subplots\n", "init_notebook_mode()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Circular links\n", "Sankey diagram links may now form cycles" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": false }, "data": [ { "link": { "source": [ 0, 1, 0, 2, 3, 3, 4 ], "target": [ 2, 3, 3, 4, 4, 5, 2 ], "value": [ 8, 4, 2, 8, 4, 2, 3 ] }, "node": { "color": [ "blue", "blue", "blue", "blue", "blue", "blue" ], "label": [ "A1", "A2", "B1", "B2", "C1", "C2" ], "line": { "color": "black", "width": 0.5 }, "pad": 15, "thickness": 20 }, "type": "sankey" } ], "layout": { "font": { "size": 10 }, "title": "Basic Sankey Diagram" } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "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,4],\n", " target = [2,3,3,4,4,5,2],\n", " value = [8,4,2,8,4,2,3]\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", "iplot(fig, validate=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Color links by concentration \n", "Groups of links can be colored by their value (width) according to a colorscale" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": false }, "data": [ { "link": { "colorscales": [ { "colorscale": [ [ 0, "white" ], [ 1, "blue" ] ], "label": "elementA" }, { "colorscale": [ [ 0, "white" ], [ 1, "red" ] ], "label": "elementB" }, { "colorscale": [ [ 0, "white" ], [ 1, "green" ] ], "label": "elementC" }, { "label": "elementD" } ], "hovertemplate": "%{label}