Lesmis Example#

Download this notebook from GitHub (right-click to download).


URL: https://docs.bokeh.org/en/latest/docs/examples/topics/categorical/les_mis.html

import numpy as np
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

Declare data#

from bokeh.sampledata.les_mis import data

nodes = data['nodes']
names = [node['name'] for node in sorted(data['nodes'], key=lambda x: x['group'])]

N = len(nodes)
counts = np.zeros((N, N))
for link in data['links']:
    counts[link['source'], link['target']] = link['value']
    counts[link['target'], link['source']] = link['value']
    
xname, yname, color, alpha = [], [], [], []
for i, node1 in enumerate(nodes):
    for j, node2 in enumerate(nodes):
        xname.append(node1['name'])
        yname.append(node2['name'])

        alpha.append(counts[i,j])

        if node1['group'] == node2['group']:
            color.append(node1['group'])
        else:
            color.append('lightgrey')

ds = hv.Dataset((xname, yname, color, alpha), ['x', 'y', 'Cluster', 'Occurences'])
overlaid = ds.to(hv.HeatMap, ['x', 'y'], ['Occurences']).overlay()

Plot#

cmaps = ['Greys', 'Reds', 'Greys', 'Greens', 'Blues',
         'Purples', 'Oranges', 'Greys', 'Greys', 'PuRd', 'Reds', 'Greys']

combined = hv.Overlay([o.opts(cmap=cm).sort() for o, cm in zip(overlaid, cmaps)], label='LesMis Occurences')
styled = combined.opts(
    opts.HeatMap(logz=True, clim=(0.1, None), clipping_colors={'NaN':(1,1,1,0.)}, xaxis='top', xrotation=90,
                 fontsize={'ticks': '7pt', 'title': '18pt'}, invert_xaxis=True, tools=['hover'],
                 labelled=[], axiswise=True),
    opts.Overlay(height=800, width=800)
)
styled
hv.Layout([el.opts(width=300, height=300) for el in styled if len(el)>10][:-1],
          label='LesMis Large Clusters').cols(3)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).