Search code examples
python-3.xplotlyholoviewschord-diagram

How to create a Chord diagram out of this dataset format?


I have a dataset which consists of passes made and received by a player with every teammate. A sample dataset looks like this:

            ter Stegen  Pique  Rakitic  Busquets  Coutinho  Suarez  Messi  \
ter Stegen           0      8        0         2         0       1      1   
Pique               12      0        2        20         0       0      1   
Rakitic              3      3        0        13         5       2      6   
Busquets             1      1        9         0         0       0      8   
Coutinho             0      0        2         1         0       4      6   
Suarez               0      0        2         1         2       0      1   
Messi                0      2        5         1         3       4      0   
Lenglet              4      6        8         8         1       0      0   
Alba                 1      1        8         4         5       8      5   
Roberto              4     11        5         4         0       4      6   
Vidal                1     10        5         8         3       2      7   

            Lenglet  Alba  Roberto  Vidal  
ter Stegen        4     3        5      5  
Pique             9     2       10      5  
Rakitic           4     8        2      5  
Busquets          4     8        7     12  
Coutinho          0     3        0      1  
Suarez            0     5        3      3  
Messi             0     4        3      4  
Lenglet           0     4        0      4  
Alba              6     0        1      4  
Roberto           1     0        0      8  
Vidal             5     7        6      0  

How do I visualize this in the form of a chord diagram which shows the flow of passes from every player to every other? I've tried using Holoviews and Plotly but I can't crack how to work with data in this format. Any help would be appreciated.

Here's the entire code:

import pandas as pd
import holoviews as hv
from holoviews import opts, dim
from bokeh.plotting import show, output_file
import numpy as np

pd.set_option("display.max_columns",11)
hv.extension('bokeh')
hv.output(size = 200)

df = pd.read_csv(r"C:\Users\ADMIN\Desktop\Abhishek\BarLiv.csv")
df = df.set_index("0")
df.index.name = None
#print(df)


# Declare a gridded HoloViews dataset and call dframe to flatten it
players = list(df.columns)
data = hv.Dataset((players, players, df), ['source', 'target']).dframe()
#print(players)

# Now create your Chord diagram from the flattened data
chord = hv.Chord(data)
chord.opts(
    node_color='index', edge_color='source', label_index='index', 
    cmap='Category10', edge_cmap='Category10', width=500, height=500)

output_file('chordtest.html')
show(hv.render(chord))

Edit 1: Here's what I'm getting after implementing @philippjfr's solution enter image description here


Solution

  • HoloViews has provides a neat little trick that makes this pretty easy, you can declare a gridded Dataset from your dataframe and then flatten it:

    df = pd.read_csv('/Users/philippjfr/Downloads/BarLiv.csv', index_col=0)
    
    # Declare a gridded HoloViews dataset and call dframe to flatten it
    data = hv.Dataset((list(df.columns), list(df.index), df),
                      ['source', 'target'], 'value').dframe()
    
    # Now create your Chord diagram from the flattened data
    chord = hv.Chord(data)
    chord.opts(
        node_color='index', edge_color='source', label_index='index', 
        cmap='Category10', edge_cmap='Category10', width=500, height=500)
    

    enter image description here