Chord Diagram in Python
Asked Answered
M

5

7

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.

Mason answered 13/6, 2021 at 19:15 Comment(2)
video from @ScottBoston is not available.Labionasal
You can create a chord diagram in d3js using python. See also this post: #65344803Hennessy
S
5

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.

Sword answered 18/8, 2022 at 10:0 Comment(1)
HoloViews worked very well for me. Frankly, I was amazed by the capabilities of HoloViews and coding in the Grammar of Graphics framework is a treat. Thank you and upvoted!Hardner
S
3

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
Straighten answered 16/6, 2021 at 7:28 Comment(1)
Thanks, this was helpful! The Bokeh library has deprecated support for chords but HoloViews has taken up from where Bokeh left off and the chord diagrams in HoloViews are a treat - as beautiful and they are elgant and informative. Thanks and upvoted.Hardner
C
2

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

Clabo answered 14/8, 2023 at 5:47 Comment(0)
H
1

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

Hennessy answered 11/11, 2022 at 15:23 Comment(0)
K
0

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

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

Krystin answered 30/8, 2022 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.