0

我有一个多向图,我想在 networkx 中绘制,但我无法让它工作。它需要是多向图的原因是交易可以双向进行。这是我的代码:

import pandas as pd
import numpy as np
import networkx as nx
from itertools import combinations,permutations

names = ['Mike','Bob','Phil']
permutations = list(permutations(names, 2))

col1 = [val[0] for val in permutations]
col2 = [val[1] for val in permutations]
transactions = [1,3,5,3,9,2]

df = pd.DataFrame({'Sender':col1,'Recipient':col2,'Num_Transactions':transactions})

所以 df 看起来像这样:

在此处输入图像描述

现在这是我用来创建和绘制图表的代码:

G = nx.MultiDiGraph()
G.add_nodes_from(names)

E = list(zip(df['Sender'].tolist(),df['Recipient'].tolist(),df['Num_Transactions'].tolist()))

G.add_weighted_edges_from(E)

def draw_graph(G, nodes_position,weight):
    nx.draw(G,nodes_position, with_labels=True, font_size=15, node_size=400,edge_color='gray',arrowsize=10)
    if weight==True:
        edge_labels = nx.get_edge_attributes(G,'weight')
    nx.draw_networkx_edge_labels(G,nodes_position, edge_labels=edge_labels)

draw_graph(G,nx.circular_layout(G),True)

这是我收到的错误消息:

在此处输入图像描述

我该如何解决?

4

0 回答 0