在Python中绘制有向图的方式可以分别显示所有边 [英] Plotting directed graphs in Python in a way that show all edges separately
问题描述
我正在使用Python模拟在有向图上发生的过程.我想制作一个此过程的动画.
I'm using Python to simulate a process that takes place on directed graphs. I would like to produce an animation of this process.
我遇到的问题是大多数Python图形可视化库将成对的有向边组合成一个边.例如, NetworkX 在显示以下图形时仅绘制两个边缘,而我想显示四个边缘中的每一个分别:
The problem that I've run into is that most Python graph visualization libraries combine pairs of directed edges into a single edge. For example, NetworkX draws only two edges when displaying the following graph, whereas I would like to display each of the four edges separately:
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)
我想显示这样的东西,每个平行边分别绘制:
I would like to display something like this, with each parallel edge drawn separately:
问题 R中igraph中的R倒数沿处理相同的问题,但是解决方案是针对R igraph库的,而不是针对Python库的.
The question R reciprocal edges in igraph in R seems to deal with the same issue, but the solution there is for the R igraph library, not the Python one.
是否有一种简单的方法可以使用现有的Python图形可视化库生成这种样式的图?如果它可以支持多图,那将是一个额外的奖励.
Is there an easy way to produce this style of plot using an existing Python graph visualization library? It would be a bonus if it could support multigraphs.
我愿意接受调用外部程序来生成图像的解决方案.我想生成一系列动画帧,因此解决方案必须是自动化的.
I'm open to solutions that invoke an external program to produce the images. I'd like to generate a whole series of animation frames, so the solution must be automated.
推荐答案
Graphviz 工具似乎显示了不同的边缘.
The Graphviz tools appear to display distinct edges.
例如,给出以下内容:
digraph G {
A -> B;
A -> B;
A -> B;
B -> C;
B -> A;
C -> B;
}
到dot
会产生:
Graphviz的输入语言非常简单,因此您可以自己生成它,尽管搜索"python graphviz"的确打开了包括
Graphviz's input language is pretty simple so you can generate it on your own, though searching for "python graphviz" does turn up a couple of libraries including a graphviz
module on PyPI.
以下是使用graphviz
模块生成上述图形的python:
Here's python that generates the above graph using the graphviz
module:
from graphviz import Digraph
dot = Digraph()
dot.node('A', 'A')
dot.node('B', 'B')
dot.node('C', 'C')
dot.edges(['AB', 'AB', 'AB', 'BC', 'BA', 'CB'])
print(dot.source)
dot.render(file_name, view=True)
这篇关于在Python中绘制有向图的方式可以分别显示所有边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!