散景和弦图0.13中的组和标签

时间:2018-12-08 12:19:54

标签: python holoviews chord-diagram

我正在尝试使用Holoview和Bokeh制作和弦图。我可以执行第一步,as explained here;但是,我无法进行分组,也无法显示颜色和标签。 首先,我的数据* chord = *:

 [['GK', 'WD ', 2],
 ['GK', 'C ', 1],
 ['GD', 'GK ', 1],
 ['GD', 'C ', 2],
 ['GD', 'WA ', 4],
 ['WD', 'GD ', 4],
 ['WD', 'C ', 1],
 ['WD', 'WA ', 2],
 ['WD', 'GA ', 1],
 ['C', 'GD ', 1],
 ['C', 'WD ', 1],
 ['C', 'WA ', 3],
 ['C', 'GA ', 2],
 ['C', 'GS ', 10],
 ['WA', 'C ', 2],
 ['WA', 'GA ', 9],
 ['WA', 'GS ', 7],
 ['GA', 'C ', 3],
 ['GA', 'WA ', 4],
 ['GA', 'GS ', 5],
 ['GS', 'C ', 6],
 ['GS', 'WA ', 1]]

值是[源,目标,值]。 让我感到困扰的是,“和弦”图不会自动对源进行分组,如果我改用Sankey图,则会发生这种情况。 因此,我创建了我的组和标签:

group = [1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,6,6,6,7,7]
label = ['GK','GK','GD','GD','GD','WD','WD','WD','WD','C','C','C','C','C','WA','WA','WA','GA','GA','GA','GS','GS']
index =list(np.arange(0,22)

尽管如此,结果远非预期:

import numpy as np
import pandas as pd
import holoviews as hv

hv.extension('bokeh')
%output size=200
chords = pd.DataFrame(chord, columns=['source','target','value'])
%opts Chord [label_index='source' color_index='target' edge_color_index='source'] 
%opts Chord (cmap='Category20' edge_cmap='Category20')
hv.Chord(chords)

enter image description here

如果我通过

hv.Chord(chords, nodes)

我收到此错误:

AttributeError: 'DataFrame' object has no attribute 'ndims'

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果您想将nodes传递给hv.Chord(),请记住,您应该先致电hv.Dataset()来获取nodes,例如: nodes = hv.Dataset(nodes_df, 'index'),其中nodes_df是熊猫的数据框。

和下一个: chord = hv.Chord((links, nodes)).select(value=(5, None)