Sankey diagram in highcharts Python

Sankey diagram in highcharts Python

The amazing highcharts library includes Sankey diagrams. I use Python to create diagrams, typically in HEX notebooks (like Jupyter notebooks), so in this post we will work through a short example of creating a Sankey diagram in highcharts using Python.

Let’s begin by installing the library (from our Jupyter or HEX notebook).

# install highcharts-core using pip
!pip install highcharts-core        

Next, load the module:

# import catch-all .highcharts module.
from highcharts_core import highcharts        

Now we define some data that we will use for our chart, it has 3 dimensions, the from, the to, and the magnitude called weight. You’ll notice that each of the 4 from countries combines with each of the 4 to countries exactly once.

# define the data
my_iterable = [
    {'from': 'Brazil', 'to': 'Portugal', 'weight': 5},
    {'from': 'Brazil', 'to': 'France', 'weight': 1},
    {'from': 'Brazil', 'to': 'Spain', 'weight': 1},
    {'from': 'Brazil', 'to': 'England', 'weight': 1},
    {'from': 'Canada', 'to': 'Portugal', 'weight': 1},
    {'from': 'Canada', 'to': 'France', 'weight': 5},
    {'from': 'Canada', 'to': 'England', 'weight': 1},
    {'from': 'Mexico', 'to': 'Portugal', 'weight': 1},
    {'from': 'Mexico', 'to': 'France', 'weight': 1},
    {'from': 'Mexico', 'to': 'Spain', 'weight': 5},
    {'from': 'Mexico', 'to': 'England', 'weight': 1},
    {'from': 'USA', 'to': 'Portugal', 'weight': 1},
    {'from': 'USA', 'to': 'France', 'weight': 1},
    {'from': 'USA', 'to': 'Spain', 'weight': 1},
    {'from': 'USA', 'to': 'England', 'weight': 5}
]        

Finally we set up the chart and show it:

# set up the chart, feed the data, define the type
chart = highcharts.Chart(data = my_iterable, series_type = 'sankey')

# structure the series
chart.series = [{
    "keys": ["from", "to", "weight"],
    "data": my_iterable,
}]

# print chart 
# N.B.: do NOT use .display() after the object name (in HEX)
chart        

This should show a chart which responds to mouse hoover like so:

Sankey diagram, when hoovering over Portugal on the right-hand side


That’s it, let me know in the comments how it goes, if this is working and how you are using it.

Saptarshi 北斗 Masid (Sap)

Senior Designer | ex TATA Group, United Nations, WHO

8 个月

Very useful and cool visualization method

回复
Bastiaan Quast PhD

Building fully decentral comms & social media; self-contained Nostr clients ?? & mesh relays ?? (Λ)

8 个月
回复

要查看或添加评论,请登录

Bastiaan Quast PhD的更多文章

  • GenAI's marginal changes & industrial paradigms

    GenAI's marginal changes & industrial paradigms

    Fratercide, vanity, farming, the first city myths of Rome and Enoch (from the Bible) have many parallels, GenAI only…

  • hashing a number using pen and paper

    hashing a number using pen and paper

    Let’s take the following three numbers: 5,7,4, this is the data we want to hash. Now, we generate a random number (1-9)…

  • Encrypted computation with decimal numbers

    Encrypted computation with decimal numbers

    About eight months ago I wrote a series of posts with Minimal Working Examples (MWEs) on computing on numbers while…

  • Model vs. Algorithm

    Model vs. Algorithm

    As discussion about Artificial Intelligence has become a mainstream topic, we hear a lot of terms used loosely. One…

    2 条评论
  • Fully Homomorphic Encryption and Computation in Python or R

    Fully Homomorphic Encryption and Computation in Python or R

    This post builds on Encrypted Computation using Public Key Encryption — Toy Example, we now develop a toy version of a…

    3 条评论
  • Encrypted Computation using Public Key Encryption — Toy Example

    Encrypted Computation using Public Key Encryption — Toy Example

    This post builds on Computing on Encrypted Data — What, Why, Toy Example, which explains the idea of computing on…

    6 条评论
  • Understanding Modulo using Days of the Week

    Understanding Modulo using Days of the Week

    Today is Monday January 1st 2024, what day of the week will it be in 7 days? That is pretty simple, a week has seven…

社区洞察

其他会员也浏览了