4 interactive Sankey diagrams made in Python

Plotly
Plotly
Published in
3 min readSep 19, 2017

Plotly has a new member of the Plotly.js chart family: The Sankey diagram. Allow us to introduce you:

A Sankey diagram showing changing voter views. The Python code to make this chart is in this Jupyter notebook: https://plot.ly/~alishobeiri/1591/plotly-sankey-diagrams/

Sankey diagrams were invented to chart energy flows (such as through a steam engine). As an example, you might start with 100% of a fuel’s energy on the left side of the diagram, then X% is lost to friction, Y% is lost to compressing the fuel, and Z% is used for propulsion — these losses are shown on the right side of the diagram. Connecting the left and the right sides are ribbons whose widths are proportional to the flow magnitude. This simplified example would show 3 ribbons for 3 energy flows, but practical Sankey diagrams can have dozens (or hundreds) of ribbons. Sankey diagrams are still used in science and engineering for charting energy flows.

Today, Sankey diagrams are commonly seen charting the flow of website traffic. Consider the case where 1,000 website visitors start on 3 landing pages, then trickle off to hundreds of sub-pages before they leave the site entirely. Analysts use these diagrams to understand which pages get the most traffic, how the traffic arrives, and where the visitors go afterwards.

Plotly Sankey diagrams come supercharged with a few extra features:

  • Hover labels — Add labels to sources, sinks, and flows so that your audience knows what they’re seeing when inspecting a complicated Sankey diagram.
  • Like all Plotly charts, there are open-source interfaces to make Sankey diagrams in R, Python, or JavaScript.
  • You can track the flow of individual items through a Plotly Sankey diagram. For example, the screenshot below shows one of 400 drones highlighted through all stages of its lifecycle. This tracking feature could be useful in manufacturing: Imagine tracking thousands of battery cells through the stages of an electric vehicle manufacturing line. The Sankey diagram could start with the delivery of the cells at the factory and end when they are installed in a completed car.
Individual items can be tracked through the flow of a Sankey diagram. In the example above, We see a Predator MQ-1B drone became part of the Predator program, was sold to the Air Force, then ended up in Afghanistan. There’s even hover text describing the fate of this drone. See this full-sized chart on plot.ly: https://plot.ly/~alishobeiri/1367.embed

Without further ado, here are 4 interactive Sankey diagrams made in Python. The Python code to make these is in this Jupyter notebook hosted on plot.ly.

Refugee Migration through Manus and Nauru

Original data and example via Bryan Brussee

400 Military Drone Crashes

Original data from the Washington Post. View full-size on plot.ly.

Changing Views of Scottish Voters

View full-size on plot.ly.

Energy Forecast for 2050

Original data and example via Mike Bostock.

You can reproduce these charts yourself in Python using this Jupyter notebook.

Footnote on terminology: Plotly’s Sankey diagrams are technically “alluvial flow diagrams,” because they can only trace flow in one direction. A true Sankey diagram can cycle a flow back to its source (such as waste heat being recycled for combustion in an engine, or regenerative breaking in an electric car). “Alluvial flow diagram” doesn’t roll off the tongue, however, so we’re sticking with the commonly recognized (and still correct) “Sankey diagram.”

--

--

Plotly
Plotly
Editor for

The low-code framework for rapidly building interactive, scalable data apps in Python.