如何在networkx图形绘制中显示周期

如何在networkx图形绘制中显示周期

本文介绍了如何在networkx图形绘制中显示周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用NetworkX构造的简单图形,如下所示:

  import networkx as nx 
import matplotlib.pyplot作为plt

G = nx.DiGraph()
G.add_edges_from([(0,1),(0,2),(1,1),(1,2)] )
nx.draw_networkx(G)
plt.show()

当我画了这张图,我看到了这张图:



图也有一个边(1,1),但它不会在图像上显示这个边。我怎样才能画出这个边缘呢?我认为这是不使用箭头的结果。这种可视化非常糟糕。如何使用箭头代替粗线?

解决方案

Graphviz在绘制箭头和自循环方面做得非常出色。 (在Matplotlib中实现的不平凡)。
下面是一个例子:

pre $ import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(\ [(0,1),(0,2),(1,1),(1,2)\])
nx.write_dot(G,'graph.dot')

#然后运行dot -Tpng graph.dot> graph.png

I have a simple graph constructed using NetworkX as follows:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from([(0,1), (0,2), (1,1), (1,2)])
nx.draw_networkx(G)
plt.show()

When I draw this graph, I get this image:

The graph also has an edge (1,1) but it does not show this edge on image. How can i draw this edge, too? I think this is the result of not using arrows. This visualization is really very bad. How can I use arrows instead of this bold lines?

解决方案

Graphviz does a great job of drawing arrows and self loops. (Non-trivial to implement in Matplotlib).Here is an example:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(\[(0,1), (0,2), (1,1), (1,2)\])
nx.write_dot(G,'graph.dot')

# then run dot -Tpng graph.dot > graph.png

这篇关于如何在networkx图形绘制中显示周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:46