Interactive Chord Diagrams

Preamble

In [1]:
from chord import Chord

Introduction

In a chord diagram (or radial network), entities are arranged radially as segments with their relationships visualised by arcs that connect them. The size of the segments illustrates the numerical proportions, whilst the size of the arc illustrates the significance of the relationships1.

Chord diagrams are useful when trying to convey relationships between different entities, and they can be beautiful and eye-catching.

Get Chord Pro

Click here to get lifetime access to the full-featured chord visualization API, producing beautiful interactive visualizations, e.g. those featured on the front page of Reddit.

chord pro

  • Produce beautiful interactive Chord diagrams.
  • Customize colours and font-sizes.
  • Access Divided mode, enabling two sides to your diagram.
  • Symmetric and Asymmetric modes,
  • Add images and text on hover,
  • Access finer-customisations including HTML injection.
  • Allows commercial use without open source requirement.
  • Currently supports Python, JavaScript, and Rust, with many more to come (accepting requests).

chord pro

The Chord Package

With Python in mind, there are many libraries available for creating Chord diagrams, such as Plotly, Bokeh, and a few that are lesser-known. However, I wanted to use the implementation from d3 because it can be customised to be highly interactive and to look beautiful.

I couldn't find anything that ticked all the boxes, so I made a wrapper around d3-chord myself. It took some time to get it working, but I wanted to hide away everything behind a single constructor and method call. The tricky part was enabling multiple chord diagrams on the same page, and then loading resources in a way that would support Jupyter Notebooks.

You can get the package either from PyPi using pip install chord or from the GitHub repository. With your processed data, you should be able to plot something beautiful with just a single line, Chord(data, names).show(). To enable the pro features of the chord package, get Chord Pro.

The Dataset

The focus for this section will be the demonstration of the chord package. To keep it simple, we will use synthetic data that illustrates the co-occurrences between movie genres within the same movie.

In [2]:
matrix = [
    [0, 5, 6, 4, 7, 4],
    [5, 0, 5, 4, 6, 5],
    [6, 5, 0, 4, 5, 5],
    [4, 4, 4, 0, 5, 5],
    [7, 6, 5, 5, 0, 4],
    [4, 5, 5, 5, 4, 0],
]

names = ["Action", "Adventure", "Comedy", "Drama", "Fantasy", "Thriller"]

Chord Diagrams

Let's see what the Chord() defaults produce when we invoke the show() method.

In [3]:
Chord(matrix, names).show()
Chord Diagram

Different Colours

The defaults are nice, but what if we want different colours? You can pass in almost anything from d3-scale-chromatic, or you could pass in a list of hexadecimal colour codes.

In [4]:
Chord(matrix, names, colors="d3.schemeSet2").show()
Chord Diagram
In [5]:
Chord(matrix, names, colors=f"d3.schemeGnBu[{len(names)}]").show()
Chord Diagram
In [6]:
Chord(matrix, names, colors="d3.schemeSet3").show()
Chord Diagram
In [7]:
Chord(matrix, names, colors=f"d3.schemePuRd[{len(names)}]").show()
Chord Diagram
In [15]:
Chord(matrix, names, colors=f"d3.schemeYlGnBu[{len(names)}]").show()
In [9]:
hex_colours = ["#222222", "#333333", "#4c4c4c", "#666666", "#848484", "#9a9a9a"]

Chord(matrix, names, colors=hex_colours).show()
Chord Diagram

Label Styling

We can disable the wrapped labels, and even change the colour.

In [10]:
Chord(matrix, names, wrap_labels=False, label_color="#4c40bf").show()
Chord Diagram

Opacity

We can also change the default opacity of the relationships.

In [11]:
Chord(matrix, names, opacity=0.1).show()
Chord Diagram

Conclusion

In this section, we've introduced the chord diagram and chord package. We used the package and some synthetic data to demonstrate several chord diagram visualisations with different configurations. The chord Python package is available for free using pip install chord.


  1. Tintarev, N., Rostami, S., & Smyth, B. (2018, April). Knowing the unknown: visualising consumption blind-spots in recommender systems. In Proceedings of the 33rd Annual ACM Symposium on Applied Computing (pp. 1396-1399). 

Support this work

You can access this notebook and more by getting the e-book, Data is Beautiful.