BKS
BKS

Reputation: 2333

Chord Diagram in Python

Hi have a DataFrame along those lines:

Source   Target    Value     
A        B         10       
A        C         5    
A        D         15
A        E         20
A        F         3
B        A         3
B        G         15
F        D         13
F        E         2
E        A         20
E        D         6

And want to draw this Chord Diagram using Python:

enter image description here

I found this chord diagram in the following link: https://www.data-to-viz.com/graph/chord.html. It states that this plot is made using the circlize library (which I believe is an R library). Is there a way to do this in Python as well?

I would also like to be able to choose the color for each element (A to G in my case) and write on the circumference as shown in the example image.

Here is another DataFrame for example with the colors:

Name   Color
A      Red
B      Orange
C      Yellow
D      Green
E      Blue
F      Purple

Also an arrow tip to help distinguish the source from the target, if possible, as in the example image.

I can't find a ready available library in python that does this for me.

Upvotes: 7

Views: 12964

Answers (5)

Ailurophile
Ailurophile

Reputation: 3005

You can try plotting the chord diagram using the D3Blocks package

D3Blocks builts on the graphics of d3 javascript (d3js) to create the most visually attractive and useful charts with only a few lines of Python code! The documentation pages contains detailed information about the working of the blocks with many examples.

Example from the documentation:

Sample Input:

The input dataset is a DataFrame with three column, source, target and weight.

Code to generate Chord diagram:

# Load d3blocks
from d3blocks import D3Blocks
#
# Initialize
d3 = D3Blocks(chart='Chord', frame=False)
#
# Import example
df = d3.import_example('energy')
#
# Node properties
d3.set_node_properties(df, opacity=0.2, cmap='tab20')
d3.set_edge_properties(df, color='source', opacity='source')
#
# Show the chart
d3.show()
#
# Make some edits to highlight the Nuclear node
# d3.node_properties
d3.node_properties.get('Nuclear')['color']='#ff0000'
d3.node_properties.get('Nuclear')['opacity']=1
# Show the chart
#
d3.show()
# Make edits to highlight the Nuclear Edge
d3.edge_properties.get(('Nuclear', 'Thermal generation'))['color']='#ff0000'
d3.edge_properties.get(('Nuclear', 'Thermal generation'))['opacity']=0.8
d3.edge_properties.get(('Nuclear', 'Thermal generation'))['weight']=1000
#
# Show the chart
d3.show()

Chord Diagram:

enter image description here

Upvotes: 2

erdogant
erdogant

Reputation: 1694

See this post Creating chord diagram in Python where I demonstrate how to create a Chord chart using the D3Blocks library.

Upvotes: 1

asleep
asleep

Reputation: 4084

You could try PlotAPI (paid) - it's available as a Python package.

Here's a video on Chord diagrams from concept to Python code.

Upvotes: 0

McLawrence
McLawrence

Reputation: 5255

Building on @mportes answer,

  • The Chord library now is a paid service, included in plotapi.
  • Plotting chord diagrams in plotly seems pretty complicated and the example is only for v3.

So the solution that worked best for me is using Holoviews. Here is an example plot. I don't know if you can make arrows, but the direction from source to target is easily identifiable because the chords always have the source color.

Upvotes: 6

mportes
mportes

Reputation: 1837

The page you share the link to has a Build your own section with a link to the Python Gallery. Here you can find three alternative ways to make a chord diagram in Python:

  • using the Chord library
  • using the Bokeh library
  • using the Plotly library

Upvotes: 3

Related Questions