简体   繁体   English

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

[英]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. 我曾尝试使用HoloviewsPlotly但无法破解如何使用这种格式的数据。 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 编辑1:这是实现@philippjfr的解决方案后的内容 在此处输入图片说明

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM