Sankey Graph with HoloViews, Examples

In this notebook we will show how to make Sankey graphs using HoloViews and Bokeh Libraries.

Sankey graphs are known to be a good way to visualize markov transition matrices.

We will show also how to customize the figure and save it in a html format.

Example 1

In [1]:
import numpy as np
import holoviews as hv
hv.extension('bokeh')
In [2]:
h1=hv.Sankey([
    ['A', 'X', 5],
    ['A', 'Y', 7],
    ['A', 'Z', 6],
    ['B', 'X', 2],
    ['B', 'Y', 9],
    ['B', 'Z', 4]]
).options(width=600, height=400)
In [3]:
h1
Out[3]:

Example 2: Career paths

The Sankey graph is maybe the most suitable one when we want to show Careers paths. The following data is extracted from the 2010 Royal Society policy report entitled “The Scientific Century: securing our future prosperity”. Indeed we can learn that 53% of the PhD Student went to a Career Outside Science, 47% of the them got an Early Career Researcher. 17% of those had been in an Early Career Researcher had a Non-Academic Research and 30% became a Permanent Research Staff. Only 22% of the last category got a Professor position which represents only 0.45% of the PhD students.

In [4]:
nodes = ["PhD", "Career Outside Science",  "Early Career Researcher", "Research Staff",
         "Permanent Research Staff",  "Professor",  "Non-Academic Research"]
In [5]:
nodes
Out[5]:
['PhD',
 'Career Outside Science',
 'Early Career Researcher',
 'Research Staff',
 'Permanent Research Staff',
 'Professor',
 'Non-Academic Research']
In [6]:
enumerate(nodes)
Out[6]:
<enumerate at 0x3221de240>
In [7]:
nodes = hv.Dataset(enumerate(nodes), 'index', 'label')
In [8]:
nodes
Out[8]:
:Dataset   [index]   (label)
In [9]:
edges = [
    (0, 1, 53), (0, 2, 47), (2, 6, 17), (2, 3, 30), (3, 1, 22.5), (3, 4, 3.5), (3, 6, 4.), (4, 5, 0.45)   
]
In [10]:
edges
Out[10]:
[(0, 1, 53),
 (0, 2, 47),
 (2, 6, 17),
 (2, 3, 30),
 (3, 1, 22.5),
 (3, 4, 3.5),
 (3, 6, 4.0),
 (4, 5, 0.45)]
In [11]:
value_dim = hv.Dimension('Percentage', unit='%')
In [12]:
h2=hv.Sankey((edges, nodes), ['From', 'To'], vdims=value_dim).options(
    label_index='label', label_position='left', width=900, height=600, edge_color_index='To'
)
In [13]:
h2
Out[13]:

Let's save the graph into a html file in order to be embedded in a website

In [14]:
renderer = hv.renderer('bokeh')
In [15]:
renderer.save(h2, 'h2')
In [16]:
def disable_logo(plot, element):
    plot.state.toolbar.logo = None
hv.plotting.bokeh.ElementPlot.finalize_hooks.append(disable_logo)
In [17]:
h2a=h2.opts(plot=dict(finalize_hooks=[disable_logo]))
In [18]:
h2a
Out[18]:

And we will save in a html file

In [19]:
renderer.save(h2a, 'h2a')
In [ ]: