使用python将分层节点的字典绘制为树形图

时间:2017-01-15 09:26:38

标签: python dendrogram

我有以下字典,我需要将其视为树形图。

d = {'L0': ['L5_0', 'L5_1', 'L5_2', 'L5_3'],
         'L10_0': ['L20_0'],
         'L10_1': ['L20_1', 'L20_7'],
         'L10_2': ['L20_2', 'L20_5'],
         'L10_3': ['L20_4', 'L20_10'],
         'L10_4': ['L20_9'],
         'L10_5': ['L20_3'],
         'L10_6': ['L20_6'],
         'L10_7': ['L20_18'],
         'L10_8': ['L20_15', 'L20_17'],
         'L10_9': ['L20_8'],
         'L20_0': ['L40_3'],
         'L20_1': ['L40_5'],
         'L20_10': ['L40_4'],
         'L20_11': ['L40_11'],
         'L20_12': ['L40_23'],
         'L20_13': ['L40_8'],
         'L20_14': ['L40_18'],
         'L20_15': ['L40_19', 'L40_38'],
         'L20_16': ['L40_10', 'L40_20', 'L40_36'],
         'L20_17': ['L40_30'],
         'L20_18': ['L40_26'],
         'L20_19': ['L40_22'],
         'L20_3': ['L40_6', 'L40_7'],
         'L20_4': ['L40_12', 'L40_15'],
         'L20_6': ['L40_17'],
         'L20_7': ['L40_13'],
         'L20_8': ['L40_9'],
         'L20_9': ['L40_1', 'L40_16'],
         'L40_0': [],
         'L40_1': [],
         'L40_10': [],
         'L40_11': [],
         'L40_12': [],
         'L40_13': [],
         'L40_14': [],
         'L40_15': [],
         'L40_16': [],
         'L40_17': [],
         'L40_18': [],
         'L40_19': [],
         'L40_2': [],
         'L40_20': [],
         'L40_21': [],
         'L40_22': [],
         'L40_23': [],
         'L40_24': [],
         'L40_25': [],
         'L40_26': [],
         'L40_27': [],
         'L40_28': [],
         'L40_29': [],
         'L40_3': [],
         'L40_30': [],
         'L40_31': [],
         'L40_32': [],
         'L40_33': [],
         'L40_34': [],
         'L40_35': [],
         'L40_36': [],
         'L40_37': [],
         'L40_38': [],
         'L40_39': [],
         'L40_4': [],
         'L40_5': [],
         'L40_6': [],
         'L40_7': [],
         'L40_8': [],
         'L40_9': [],
         'L5_0': ['L10_0', 'L10_3'],
         'L5_1': ['L10_4', 'L10_5', 'L10_6', 'L10_8'],
         'L5_2': ['L10_9'],
         'L5_3': ['L10_2'],
         'L5_4': ['L10_1', 'L10_7']})

我正在使用我在另一个stackoverflow答案中找到的代码,但是当我使用我的字典尝试它时,它会给我一个错误。

代码:

# Load required modules
import networkx as nx
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram

# Construct the graph/hierarchy
G           = nx.DiGraph(d)
nodes       = G.nodes()
leaves      = set( n for n in nodes if G.out_degree(n) == 0 )
inner_nodes = [ n for n in nodes if G.out_degree(n) > 0 ]

# Compute the size of each subtree
subtree = dict( (n, [n]) for n in leaves )
for u in inner_nodes:
    children = set()
    node_list = list(d[u])
    while len(node_list) > 0:
        v = node_list.pop(0)
        children.add( v )
        node_list += d[v]

    subtree[u] = sorted(children & leaves)

inner_nodes.sort(key=lambda n: len(subtree[n])) # <-- order inner nodes     ascending by subtree size, root is last

# Construct the linkage matrix
leaves = sorted(leaves)
index  = dict( (tuple([n]), i) for i, n in enumerate(leaves) )
Z = []
k = len(leaves)
for i, n in enumerate(inner_nodes):
    children = d[n]
    x = children[0]
    for y in children[1:]:
        z = tuple(subtree[x] + subtree[y])
        i, j = index[tuple(subtree[x])], index[tuple(subtree[y])]
        Z.append([i, j, float(len(subtree[n])), len(z)]) # <-- float is   required by the dendrogram function
        print 'Z', Z
        index[z] = k
        subtree[z] = list(z)
        x = z
        k += 1

# Visualize
dendrogram(Z, labels=leaves)
plt.show()

当我运行它时,我收到以下错误: KeyError :(&#39; L40_13&#39;,&#39; L40_5&#39;)

有人可以帮忙吗? 感谢

整个追溯:

KeyError                                  Traceback (most recent call last)
<ipython-input-105-ed0150b8f170> in <module>()
     43         z = tuple(subtree[x] + subtree[y])
     44         print 'z',z
---> 45         i, j = index[tuple(subtree[x])], index[tuple(subtree[y])]
     46         print 'i',i, 'j',j
     47         Z.append([i, j, float(len(subtree[n])), len(z)]) # <-- float is required by the dendrogram function

KeyError: ('L40_13', 'L40_5')

1 个答案:

答案 0 :(得分:0)

通过在此行中添加排序功能,我可以为我的字典完成这项工作:

z = tuple(subtree[x] + subtree[y])

创建

z = tuple(sorted(subtree[x] + subtree[y]))