如何使用 matplotlib 或 graphviz 在 networkx 中绘制多图

如何使用 matplotlib 或 graphviz 在 networkx 中绘制多图

问题描述:

当我将多图 numpy 邻接矩阵传递给 networkx 时(使用 from_numpy_matrix 函数)然后尝试使用 matplotlib 绘制图形,它忽略了多条边.

when I pass multigraph numpy adjacency matrix to networkx (using from_numpy_matrix function) and then try to draw the graph using matplotlib, it ignores the multiple edges.

如何让它也绘制多条边?

how can I make it draw multiple edges as well ?

Graphviz 在绘制平行边方面做得很好.您可以通过编写一个点文件然后用 Graphviz 处理(例如下面的neato布局)来将它与 NetworkX 一起使用.除了 NetworkX 之外,您还需要 pydot 或 pygraphviz

Graphviz does a good job drawing parallel edges. You can use that with NetworkX by writing a dot file and then processing with Graphviz (e.g. neato layout below). You'll need pydot or pygraphviz in addition to NetworkX

In [1]: import networkx as nx

In [2]: G=nx.MultiGraph()

In [3]: G.add_edge(1,2)

In [4]: G.add_edge(1,2)

In [5]: nx.write_dot(G,'multi.dot')

In [6]: !neato -T png multi.dot > multi.png

在 NetworkX 1.11 和更新版本上,nx.write_dot 不符合 networkx github 上的问题.解决方法是使用

On NetworkX 1.11 and newer, nx.write_dot doesn't work as per issue on networkx github. The workaround is to call write_dot using

from networkx.drawing.nx_pydot import write_dot

from networkx.drawing.nx_agraph import write_dot