python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > networkx绘制MultiDiGraph

Python实现用networkx绘制MultiDiGraph

作者:EULE

这篇文章主要介绍了Python实现用networkx绘制MultiDiGraph方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

networkx绘制MultiDiGraph

美赛作图需要,想表现两个节点之间的双向关系,但是网上关于MultiDiGraph的内容很少,翻出去查了一下多向图的画法

记录一下

import matplotlib.pyplot as plt
import networkx as nx
G = nx.MultiDiGraph()
#中间过程略
nx.draw(G,pos,connectionstyle='arc3, rad = 0.2',width=[float(v['weight']) for (r,c,v) in G.edges(data=True)])
#connectionstyle='arc3, rad = 0.2'arc控制双向,rad调线条弧度

最终效果

最终效果↑

画图部分完整的代码

```python
G = nx.MultiDiGraph()
row=np.array(['D1','D2','D3','D4','D5','F1','F2','F3','M1','M6','M3','M4','M5','G1'])
G.add_nodes_from(['D1','D2','D3','D4','D5','F1','F2','F3','M1','M6','M3','M4','M5','G1'])
value=np.array(count)
for j in range(0,14):
    for i in range(0,14):
        if i!=j:
            G.add_weighted_edges_from([(row[j],row[i],value[14*j+i])])#边的起点,终点,权重            
        else:
            pass
node_sizes = [44,22,29,20,35,11,32,20,41,22,57,10,11,20]
node_sizesnew=[]
for i in node_sizes:
    i=i*20
    node_sizesnew.append(i)  
pos={'D1':(37.29,77.42),'D2':(31.73,59.91),'D3':(40.47,27.53),'D4':(58.82,15.55),'D5':(46.29,89.65),'F1':(72,52),'F2':(61.64,45.73),'F3':(59.22,59.78),'G1':(11.64,49.73),'M1':(50.55,49.27),'M3':(46.56,56.18),'M4':(74.75,74.25),'M5':(58.25,46),'M6':(49.09,61.09)}
nx.draw_networkx_nodes(G,pos,node_size=node_sizesnew,alpha=0.4)
nx.draw_networkx_labels(G,pos,font_size=8,)
nx.draw(G,pos,edge_color='lightseagreen',alpha=0.5,connectionstyle='arc3, rad = 0.2',width=[float(v['weight']) for (r,c,v) in G.edges(data=True)])
edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in G.edges(data=True)])
plt.savefig("D:/MATCH52.png",dpi=200, bbox_inches='tight')
show()
print('finish')

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文