Skip to main content

Python wrapper around d3-chord

Project description

Example Image

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.

The Chord Package

I wanted to do a section on Chord Diagrams for my book, Data Is Beautiful.

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 Lab.

The primary support is for Jupyter Lab (not the older Jupyter Notebook).

Installation

Available on https://pypi.org/chord through pip:

pip install chord

Usage

Python

Python (HTML file)

Chord(matrix, names).to_html()

Jupyter Lab (Notebook)

Chord(matrix, names).show()

Defaults

Chord(self,
        matrix,
        names,
        colors="d3.schemeSet1",
        opacity=0.8,
        padding=0.01,
        width=700,
        label_color="#454545",
        wrap_labels=True,
        margin=0,
        credit=False,
    )

Examples

You can see the actual interactive examples on this page. The below examples are screenshots.

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.

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"]

Default Settings

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

Chord(matrix, names).show()

Example Image

You can also save it to a HTML file.

Chord(matrix, names).to_html()

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.

Chord(matrix, names, colors="d3.schemeSet2").show()

Example Image

Chord(matrix, names, colors=f"d3.schemeGnBu[{len(names)}]").show()

Example Image

Chord(matrix, names, colors="d3.schemeSet3").show()

Example Image

Chord(matrix, names, colors=f"d3.schemePuRd[{len(names)}]").show()

Example Image

Chord(matrix, names, colors=f"d3.schemeYlGnBu[{len(names)}]").show()

Example Image

hex_colours = ["#222222", "#333333", "#4c4c4c", "#666666", "#848484", "#9a9a9a"]

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

Example Image

Label Styling

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

Chord(matrix, names, wrap_labels=False, label_color="#4c40bf").show()

Example Image

Opacity

We can also change the default opacity of the relationships.

Chord(matrix, names, opacity=0.1).show()

Example Image

Diagram Size

We can change the maximum diagram size by specifying a width.

Chord(matrix, names, width=400).show()

We can change the padding between chord segments by specifying the padding.

Chord(matrix, names, padding=0.3).show()

  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). 

Credits

  • d3-chord, Mike Bostock.
  • d3-chord gradient fills, Nadieh Bremer.
  • chord (Python), Shahin Rostami.

Example Image


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chord-0.0.12.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chord-0.0.12-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file chord-0.0.12.tar.gz.

File metadata

  • Download URL: chord-0.0.12.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for chord-0.0.12.tar.gz
Algorithm Hash digest
SHA256 ef7a10f67d39a99d473bc2c0693312bf3ac4ebc32959c72fe91b2ccb3aa6a281
MD5 abd7683c97350e7ae10d73fb53f49f1e
BLAKE2b-256 de95c014a573f52d78cb2d73532fc000b7f2da6f706c8ca3eeab4d1beb64ee53

See more details on using hashes here.

File details

Details for the file chord-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: chord-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for chord-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 982d3d85a167059bb02acbc52db437d982c0364024119cac74abb6461f5c560c
MD5 f98fb197e63964665a860f615277cfb0
BLAKE2b-256 2752ec0200bd038fba7fda57cf596ba9c562df7a8445580accff253da94c4068

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page