1 回答

TA貢獻2016條經驗 獲得超9個贊
你的問題需要一個MultiGraph
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
import pydot
from IPython.display import Image
dic_values = {"Source":[24120.0,24120.0,24120.0], "Interlocutor":[34,34,34],
? ? ? ? ? ? ? "Frequency":[446625000, 442475000, 445300000]}
session_graph = pd.DataFrame(dic_values)
sources = session_graph['Source'].unique()
targets = session_graph['Interlocutor'].unique()
#create a Multigraph and add the unique nodes
G = nx.MultiDiGraph()
for n in [sources, targets]:
? ? G.add_node(n[0])
? ??
#Add edges, multiple connections between the same set of nodes okay.?
# Handled by enum in Multigraph? ??
#Itertuples() is a faster way to iterate through a Pandas dataframe. Adding one edge per row
for row in session_graph.itertuples():
? ? #print(row[1], row[2], row[3])
? ? G.add_edge(row[1], row[2], label=row[3])? ? ? ??
? ? ? ??
#Now, render it to a file...
p=nx.drawing.nx_pydot.to_pydot(G)
p.write_png('multi.png')
Image(filename='multi.png') #optional?
這將產生以下結果:
請注意,當您使用 Graphviz/Pydot 時,節點布局會更加棘手。
添加回答
舉報