我正在使用Python来模拟在有向图上发生的过程.我想制作一个这个过程的动画.
我遇到的问题是大多数Python图形可视化库将成对的有向边组合成一个边.例如,NetworkX在显示下图时只绘制两条边,而我想分别显示四条边的每一条:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.MultiDiGraph()
G.add_edges_from([
(1, 2),
(2, 3),
(3, 2),
(2, 1),
])
plt.figure(figsize=(8,8))
nx.draw(G)
Run Code Online (Sandbox Code Playgroud)

我想显示这样的东西,每个平行边缘分开绘制:

R中的igraph中的R倒数边缘的问题似乎处理相同的问题,但是解决方案存在于R igraph库,而不是Python.
有没有一种简单的方法可以使用现有的Python图形可视化库生成这种样式的绘图?如果它可以支持多图,那将是一个奖励.
我愿意接受调用外部程序来生成图像的解决方案.我想生成一系列动画帧,因此解决方案必须自动化.
In the class networkx.MultiGraph, an edge is keyed by (u, v, key), for instance, ('n1', 'n2', 'key1'). I would like to draw edge labels (say weight, (u, v, key): 10) for MultiGraph by using the function networkx.draw_networkx_edge_labels.
However, edge labels are keyed by a two-tuple (u, v) in draw_networkx_edge_labels, instead of a 3-tuple (u, v, key), as is the case in MultiGraph. This raises ValueError: too many values to unpack.
PS: …
我想使用networkx(如果您知道更好的框架,我也想采用其他框架)来创建节点位于固定位置的抓图。同时,图形的边缘不应重叠。
我之前的代码如下:
#!/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])] = '{} -> …Run Code Online (Sandbox Code Playgroud) 我目前的networkx图表有以下代码:
import matplotlib.pyplot as plt
import networkx as nx
g = nx.Graph()
# add edges
g.add_edge("a", "b", weight=0.6)
g.add_edge("a", "c", weight=0.2)
g.add_edge("c", "d", weight=0.1)
g.add_edge("c", "e", weight=0.7)
g.add_edge("c", "f", weight=0.9)
g.add_edge("a", "d", weight=0.3)
# group edges by attribute "weight"
elarge = [
(u, v) for (u, v, d) in g.edges(data=True)
if d["weight"] > 0.5]
esmall = [
(u, v) for (u, v, d) in g.edges(data=True)
if d["weight"] <= 0.5]
# compute the positions of nodes
pos = nx.circular_layout(g) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 中的networks.draw() 函数绘制图形。尽管它不是有向图,但我的图的边缘按拓扑排序顺序排列。我想打印看起来像依赖 DAG 的图表以获得更好的可见性。目标是这样的:
我该怎么做?