在Python3中使用NetworkX创建弯曲边缘

问题描述 投票:3回答:1

我想使用networkx(如果你知道更好的话,我还想采用另一个框架)来创建一个节点位于固定位置的graps。同时,图形的边缘不应重叠。

我之前的代码如下所示:

#!/usr/bin/env python3

import networkx as nx
import matplotlib.pyplot as plt

# Graph data
names = ['A', 'B', 'C', 'D', 'E']
positions = [(0, 0), (0, 1), (1, 0), (0.5, 0.5), (1, 1)]
edges = [('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('D', 'A')]

# Matplotlib figure
plt.figure('My graph problem')

# Create graph
G = nx.MultiDiGraph(format='png', directed=True)

for index, name in enumerate(names):
    G.add_node(name, pos=positions[index])

labels = {}
for edge in edges:
    G.add_edge(edge[0], edge[1])
    labels[(edge[0], edge[1])] = '{} -> {}'.format(edge[0], edge[1])

layout = dict((n, G.node[n]["pos"]) for n in G.nodes())
nx.draw(G, pos=layout, with_labels=True, node_size=300)
nx.draw_networkx_edge_labels(G, layout, edge_labels=labels)

plt.show()

并给出以下结果

enter image description here

如何确保边缘“圆整”以使它们不重叠?

python matplotlib graph networkx directed-graph
1个回答
1
投票

正如保罗所提到的,现在可以选择在FancyArrowPatch中使用draw_networkx_edges,尽管它仅适用于有向图并且也非常慢。

为了它的价值,我打包了一些旧的代码,它使用bezier包从NetworkX图形(或任何边缘列表,真的)生成漂亮的曲线边缘并绘制它们。它可能有用:https://github.com/beyondbeneath/bezier-curved-edges-networkx

样本图像,使用SNAP Facebook数据集和ForceAtlas2布局:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.