如何在networkx图形绘制中显示循环 [英] How to show cycles in networkx graph drawing

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

问题描述

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

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:

该图也有一条边 (1,1),但它没有在图像上显示这条边.我怎样才能画出这条边呢?我认为这是不使用箭头的结果.这种形象化真的很糟糕.如何使用箭头而不是粗线?

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 在绘制箭头和自循环方面做得很好.(在 Matplotlib 中实现非常重要).下面是一个例子:

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图形绘制中显示循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆