In [1]: import networkx as nx
In [2]: G=nx.MultiGraph()
In [3]: G.add_edge(1,2)
In [4]: G.add_edge(1,2)
In [5]: nx.write_dot(G,'multi.dot')
In [6]: !neato -T png multi.dot > multi.png
在NetworkX 1.11和更高版本中,nx.write_dot无法按照issue on networkx github工作。解决方法是使用以下命令调用write_dot from networkx.drawing.nx_pydot import write_dot 或 from networkx.drawing.nx_agraph import write_dot
import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
import random as rd
column_from = 'from_here'
column_to = 'to_there'
column_attribute = 'edges_count'
# test data
pdf = pd.DataFrame([
['a', 'b', 3],
['b', 'a', 1],
['a', 'c', 1],
['b', 'c', 1],
['a', 'd', 1],
['e', 'b', 2],
['c', 'f', 1],
['f', 'g', 1]],
columns=[column_from, column_to, column_attribute])
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(pdf)
def get_adjacency_matrix(pdf):
id_set = set(pdf[column_from].drop_duplicates().values.tolist() +
pdf[column_to].drop_duplicates().values.tolist())
id_dict_kv = {k : v for k, v in enumerate(id_set)}
id_dict_vk = {v : k for k, v in id_dict_kv.items()}
count = len(id_set)
adjacency_matrix = np.zeros([count, count], dtype='int32')
for row in pdf.itertuples():
index_from = id_dict_vk[getattr(row, column_from)]
index_to = id_dict_vk[getattr(row, column_to)]
adjacency_matrix[index_from, index_to] += getattr(row, column_attribute)
label_mapping = id_dict_kv
return adjacency_matrix, label_mapping
def pdf_to_MDG(pdf):
adjacency_matrix, label_mapping = get_adjacency_matrix(pdf)
G = nx.from_numpy_matrix(adjacency_matrix, parallel_edges=True, create_using=nx.MultiDiGraph())
G = nx.relabel_nodes(G, label_mapping)
return G
MDG = pdf_to_MDG(pdf)
edges_data = MDG.edges.data(column_weight)
print(edges_data)
#—————————————————————————————just see the below: draw MultiDiGraph—————————————————————————————————
pos = nx.spring_layout(MDG, seed = 1)
nx.draw(MDG, pos, with_labels=True, edge_color = (1,1,1))
for e in MDG.edges:
plt.gca().annotate("",
xy=pos[e[1]],
xycoords='data',
xytext=pos[e[0]],
textcoords='data',
arrowprops=dict(arrowstyle="->", color="0",
shrinkA=15, shrinkB=15,
patchA=None, patchB=None,
connectionstyle="arc3,rad=rrr".replace('rrr',str(rd.random()*0.5+0.1)))
)
plt.axis('off')
plt.show()
输出:
from_here to_there edges_count
0 a b 3
1 b a 1
2 a c 1
3 b c 1
4 a d 1
5 e b 2
6 c f 1
7 f g 1
[('c', 'f', 1), ('e', 'b', 1), ('e', 'b', 1), ('b', 'c', 1), ('b', 'a', 1), ('f', 'g', 1), ('a', 'c', 1), ('a', 'd', 1), ('a', 'b', 1), ('a', 'b', 1), ('a', 'b', 1)]
4条答案
按热度按时间pw9qyyiw1#
Graphviz在绘制平行边方面做得很好。你可以在NetworkX上使用它,写一个点文件,然后用Graphviz处理(例如下面的整洁布局)。除了NetworkX,你还需要pydot或pygraphviz
在NetworkX 1.11和更高版本中,
nx.write_dot
无法按照issue on networkx github工作。解决方法是使用以下命令调用write_dot
from networkx.drawing.nx_pydot import write_dot
或
from networkx.drawing.nx_agraph import write_dot
aydmsdu92#
您可以使用计算的节点位置直接使用matplotlib。
7bsow1i63#
您可以使用pyvis包。
我只是从我的Jupyter笔记本实际项目复制粘贴此代码。
result
zd287kbt4#
参考atomh33ls的答案
输出:
the output img