如何用这种数据集格式创建一个Chord图?

时间:2019-05-02 15:57:58

标签: python-3.x plotly holoviews chord-diagram

我有一个数据集,其中包含玩家与每个队友进行的接发。样本数据集如下所示:

            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  

我如何以和弦图的形式形象地显示每个玩家到另一个玩家的传球流程?我尝试使用HoloviewsPlotly,但无法破解如何使用这种格式的数据。任何帮助将不胜感激。

这是完整的代码:

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))

编辑1:这是实施@philippjfr解决方案后的内容 enter image description here

1 个答案:

答案 0 :(得分:1)

HoloViews提供了一个整洁的小技巧,使这变得很容易,您可以从数据框中声明网格化的数据集,然后将其展平:

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