How to Plot a Chord Diagram Using Python?

Do you know which data visualization tool is ideal for interrelation analysis?

Is a bar chart a better choice for this? Certainly not. 

The Chord Diagram is the best visualization tool for interrelation analysis. This graphical tool depicts how one entity is linked to others quantitatively. We can glean valuable information from this diagram.

This blog post will explain what a chord diagram is, how to read one, how data scientists use it, and how to make your own using Python, the powerful programming language. Keep reading to know more.

What is a Chord Diagram?

The word “chord” comes from the vocabulary used in the geometrics field. A chord is a geometric line segment with both endpoints on the circle.

Chord diagrams are data visualization comprising connected lines to show the relationships between two data points. Chord diagrams visualize the flow of information between different systems or show the relationships between different items in a dataset.

Chord diagrams can visualize any relationships, such as the flow of traffic between cities, the migration patterns of animals, or the movement of people between different countries. 

Are you confused?

Let us understand what a chord diagram is with a simple diagram. 

The diagram shows that the circle is segmented into three parts. The data is in the form of a matrix. Each part is called a Node labeled A, B, and C.

Next, you can see a ribbon from the A Node to the C node represented by Blue color; This is called Chord Links. A has a relationship between B with 10 points and B links with C 10 points.

How to Read a Chord Diagram?

Now let’s understand how to read the real-time Chord Diagram.

Reading a Chord Diagram is not as complicated as you think. It is straightforward to analyze information from the aesthetic Chord Diagram. In this section, you can investigate viewing patterns of how the audience shifts watching between avenger films.

There are six avenger films in segments in nodes. You can see it in the below video.

The node’s width shows the number of the audience that watches the particular movies. For example, 16 audiences watch Captain America Movie in the chord diagram, and 12 audiences see The Hulk Movie. So the Width of The Captain American Node is large than the width of the Hulk Movie

Now let’s analyze the relationship between two movie watchers. Colored ribbons called chord links shows the relationship between movie watchers. For example, two fans of Captain America movies move to watch Hulk Movie. Likewise, two fans of the Hulk Movie shift to Captain America.

The ribbons are colored for easier identification—red color for Hawk Movie, Green Color for Captain America, etc. From the chord diagram, you can get the following information. 

  1. Three Fans of Captain America Movie shift to Thor movie,
  2. Three Fans of Iron Man Movie shift to Hawkeyes movie. 

So look, it is straightforward to read, and quickly you can capture the interrelation information.

How to Plot a Chord Diagram using Python?

Okay! Enough of Theory!

Let us now plot Chord Diagram with a few lines of the python code. The Python library you will use to generate the diagram is the Matplotlib chord diagram library. 

We will use the exports of goods and services of five countries as data for plotting the chord diagram.

The source of the data is the IMF website. You can see the table of export details below:

US DOLLARS IN MILLIONS
CountriesAustraliaCanadaChinaGermanyIndia
Australia01,459.35100,542.362,593.7930,991.59
Canada1591.56018764.674741.342581.97
China53509.1542126.46086997.6166779.54
Germany9731.910667.21110373012233.01
India3465.592811.418921.637657.330
source

Now let us install the library by running the following command in a terminal:

pip install mpl-chord-diagram

Once the chord diagram library is installed, then you can enter the following command to import the same. You have to import matplotlib.pyplot library for plotting the diagram.

from mpl_chord_diagram import chord_diagram
import matplotlib.pyplot as plt

The export data is stored in the “mat” variable list data type. Each row of the table is broken down into five separate lists (see nested lists).

For example, in the first row, export details below to Australia exporting to various countries of Canada, Germany, China, and India.

Likewise, the following list shows export details of Canada exporting to Australia, Germany, China, and India. 

mat = [
     [0,1459.35,100542.36,2593.79,30991.59],
     [1591.56,0,18764.67,4741.34,2581.97],
     [53509.15,42126.46,0,86997.61,66779.54],
     [9731.9,10667.21,110373,0,12233.01],
     [3465.59,2811.4,18921.63,7657.33,0],
 ]

The country names are stored in a list country_name variable.

country_names = ["Australia","Canada","China","Germany","India"]

After creating the data, it’s time to plot the chord diagram using the chord_diagram function. 

chord_diagram(mat, names=country_names)

The two parameters of the chord diagram functions are matrix data and names of the nodes, which will be displayed in the diagram.

Finally, the below commands display the Diagram of the Chord Diagram.

plt.show()

Now, let’s read the chord diagram to gather interesting information.

  1. China is a Huge exporting country compared to others in the diagram. You observe through a bigger node size.
  2. Canada is the least exporting country compared to the other four, as shown by its smaller nodes,
  3. China’s exports to Germany are much larger than to other countries
  4. India’s exports to China exceed its imports of goods or services.

Full Python Example Chord Diagram

Here’s the code discussed previously in full for copy&paste:

from mpl_chord_diagram import chord_diagram
import matplotlib.pyplot as plt
mat = [
     [0,1459.35,100542.36,2593.79,30991.59],
     [1591.56,0,18764.67,4741.34,2581.97],
     [53509.15,42126.46,0,86997.61,66779.54],
     [9731.9,10667.21,110373,0,12233.01],
     [3465.59,2811.4,18921.63,7657.33,0],
 ]
country_names = ["Australia","Canada","China","Germany","India"]
chord_diagram(mat, names=country_names)
plt.show()

Typical usages of Chord Diagram 

Let us see how a Data visualization expert uses the chord diagram to explain their analysis.

Nadieh Bremer

She is an astronomer graduate who converted to Data Scientist and Data Visualization Designer. She found her passion for data visualization after working for a consultancy & fintech company. Currently, Nadieh is working as a data visualization freelancer, and her blog name is “Visual Cinnamon.”

She was awarded as Best Individual in the Information is Beautiful Award in 2017. Her primary focus is creating a unique interactive data visualization that engages audiences. (source)

She published an article about using data-storytelling with chord diagrams in her blog.

In this article, she described how she had enlightened the audience about her analysis using the Chord diagram. She has created beautiful Chord diagram visuals on the Switching behavior between phone brands of the Dutch.

This diagram explains the relationships between switching phone brands to the audience. She finally concludes by following insights about this interesting analysis. Samsung and Apple phone brands acquire customers from Nokia and LG brands. Apple is losing its few customers. HTC captures Nokia and LG customers but loses its customers to Samsung & Huawei.  

Dénes Csala

He is a researcher and thinker who is interested in exploring Data Visualisation. Denes is an assistant professor at Lancaster University in the UK and also visiting assistant professor at Babeș-Bolyai University in Romania for Data Visualisation and Business Intelligence. He is the founder of Dénes Csala Data Consulting, where he provides customs Data Visualisation services. Denis has created a chord diagram on World Refugee flows between 1951-and 2014.

This diagram explores refugee migration between countries on the global and domestic levels. Also, it includes internally displaced persons in the data. He has found out following insights on the Chord diagram. The total population of the world’s refugees is 51.6 million, which includes 12.7 million IDPs. In 2014, Syria had the largest population of refugees, i.e., 11.5 million people, including 7.8 million IDPs. Three regions of Colombia, Iraq, and DRC  have a vast population of IDP 6,3.8 and 3.3 million.

Sources: You can find more information here:

Summary

Chord Diagrams are an excellent tool for analyzing interrelationships.

We can obtain critical information that other tools cannot.

This blog taught you how to read and interpret chord diagrams and how renowned data scientists use them.

I hope you enjoyed reading this article—join us for more Python content here: