Chord#

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


Title
Graph Element
Dependencies
Matplotlib
Backends
Matplotlib
Bokeh
import pandas as pd
import holoviews as hv

from holoviews import dim, opts
from bokeh.sampledata.les_mis import data

hv.extension('matplotlib')
hv.output(fig='svg', size=200)

The Chord element allows representing the inter-relationships between data points in a graph. The nodes are arranged radially around a circle with the relationships between the data points drawn as arcs (or chords) connecting the nodes. The number of chords is scaled by a weight declared as a value dimension on the Chord element.

If the weight values are integers, they define the number of chords to be drawn between the source and target nodes directly. If the weights are floating point values, they are normalized to a default of 500 chords, which are divided up among the edges. Any non-zero weight will be assigned at least one chord.

The Chord element is a type of Graph element and shares the same constructor. The most basic constructor accepts a columnar dataset of the source and target nodes and an optional value. Here we supply a dataframe containing the number of dialogues between characters of the Les Misérables musical. The data contains source and target node indices and an associated value column:

links = pd.DataFrame(data['links'])
print(links.head(3))
   source  target  value
0       1       0      1
1       2       0      8
2       3       0     10

In the simplest case we can construct the Chord by passing it just the edges:

hv.Chord(links)

To add node labels and other information we can construct a Dataset with a key dimension of node indices.

nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index')
nodes.data.head()
name group
0 Myriel 1
1 Napoleon 1
2 Mlle.Baptistine 1
3 Mme.Magloire 1
4 CountessdeLo 1

Additionally we can now color the nodes and edges by their index and add some labels. The labels, node_color and edge_color options allow us to reference dimension values by name.

hv.Chord((links, nodes)).select(value=(5, None)).opts(
    opts.Chord(cmap='Category20', edge_color=dim('source').astype(str), labels='name', node_color=dim('index').astype(str)))
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).