简体   繁体   English

Networkx:可视化 MultiGraph 时的重叠边缘

[英]Networkx: Overlapping edges when visualizing MultiGraph

The following multigraph plots correctly (ie parallel edges do not overlap) using graphviz neato to generate a png (as shown in this answer )以下多图正确绘制(即平行边不重叠)使用graphvizneato生成png(如本答案所示)

import networkx as nx
nx.MultiGraph ([(1,2),(1,2),(1,2),(3,1),(3,2)])
nx.write_dot(Gm,'multi.dot')
!neato -T png multi.dot > multi.png

However using the draw function of Networkx doesn't do the trick但是使用 Networkx 的绘图功能并不能解决问题

nx.draw_graphviz(Gm,prog='neato')

Is it possible to prevent overlapping edges using the draw methods from Networkx?是否可以使用 Networkx 的绘制方法来防止重叠边缘?

Thanks谢谢

Unfortunately not.不幸的是没有。 It is technically possible to do but so far nobody has written the code.这在技术上是可行的,但到目前为止还没有人编写代码。

You can use matplotlib directly using the node positions you have calculated.您可以使用您计算出的节点位置直接使用 matplotlib。

G=nx.MultiGraph ([(1,2),(1,2),(1,2),(3,1),(3,2)])
pos = nx.random_layout(G)
nx.draw_networkx_nodes(G, pos, node_color = 'r', node_size = 100, alpha = 1)
ax = plt.gca()
for e in G.edges:
    ax.annotate("",
                xy=pos[e[0]], xycoords='data',
                xytext=pos[e[1]], textcoords='data',
                arrowprops=dict(arrowstyle="->", color="0.5",
                                shrinkA=5, shrinkB=5,
                                patchA=None, patchB=None,
                                connectionstyle="arc3,rad=rrr".replace('rrr',str(0.3*e[2])
                                ),
                                ),
                )
plt.axis('off')
plt.show()

在此处输入图片说明

Well I know its probably not what you're looking for, but I was facing a similar problem where I wanted to have a directed graph where the edge between two nodes had a different weight depending on the direction (whether it was going into or out of the node) and the work around I did was I used a different color for each edge and decreased the opacity for one of them so it would show even if they overlap.好吧,我知道它可能不是您要找的东西,但是我遇到了类似的问题,我想要一个有向图,其中两个节点之间的边根据方向(无论是进还是出)具有不同的权重节点),我所做的工作是我为每条边使用了不同的颜色,并降低了其中一个的不透明度,这样即使它们重叠也会显示出来。 I only needed two edges between my two nodes so it did the trick for me.我的两个节点之间只需要两条边,所以它对我有用。

G = nx.DiGraph()
G.add_nodes_from([0,1])
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, node_color = 'r', node_size = 100, alpha = 1)
nx.draw_networkx_edges(G, pos, edgelist = [(0,1)], width = 2, alpha = 0.5, edge_color='b')
nx.draw_networkx_edges(G, pos, edgelist= [(1,0)], width = 1, alpha = 1)
plt.axis('off')
plt.show() 

在此处输入图片说明

An improvement to the answer above is adding the connectionstyle argument to nx.draw:对上述答案的改进是将connectionstyle参数添加到 nx.draw:

import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([0,1])
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, connectionstyle='arc3, rad = 0.1', node_color = 'r', node_size = 100, alpha = 1)
nx.draw_networkx_edges(G, pos,connectionstyle='arc3, rad = 0.1', edgelist = [(0,1)], width = 2, alpha = 0.5, edge_color='b')
nx.draw_networkx_edges(G, pos,connectionstyle='arc3, rad = 0.1', edgelist= [(1,0)], width = 1, alpha = 1)
plt.axis('off')
plt.show() 

在此处查看结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM