简体   繁体   English

使用networkx在python中绘制无向图

[英]Plotting undirected graph in python using networkx

I am using networkx to plot graph in python. 我正在使用networkx在python中绘制图形。 However, my output is too dense. 但是,我的输出太密集了。 Is there any ways to sparse the graph? 有什么方法可以稀疏图吗? Below is my command in python.Thanks 下面是我在python中的命令。

    pos=nx.spring_layout(self.G)
    nx.draw_networkx_nodes(self.G,pos)
    edge_labels=dict([((u,v,),d['weight'])
             for u,v,d in self.G.edges(data=True)])
    nx.draw_networkx_labels(self.G,pos, font_size=20,font_family='sans-serif')
    nx.draw_networkx_edges(self.G,pos)
    plt.axis('off')
    #nx.draw_networkx_labels(self.G,pos, font_size=20,font_family='sans-serif')
    nx.draw_networkx_edge_labels(self.G,pos,edge_labels=edge_labels)
    nx.draw(self.G,pos, edge_cmap=plt.cm.Reds)

    plt.show()

样本图

You can also try Graphviz via PyDot (I prefer this one) or PyGraphviz . 您也可以通过PyDot (我更喜欢这一点)或PyGraphviz尝试Graphviz

In case you are interested in a publication-ready result, you can use the toolchain networkx -> pydot + dot -> dot2tex + dot -> dot2texi.sty -> TikZ . 如果您对发布就绪的结果感兴趣,可以使用工具链networkx > pydot + dot -> dot2tex + dot -> dot2texi.sty > TikZ Instead of this fragile toolchain, you can alternatively export directly to TikZ with nx2tikz (I've written it, so I'm biased) and use the graph layout algorithms that have been relatively recently added to TikZ . 除了这个脆弱的工具链之外,您还可以使用nx2tikz直接导出到TikZ (我已经编写了它,所以我有偏见),并且可以使用最近相对于TikZ添加的图形布局算法。

After I check some information from the documentation here: http://networkx.github.io/documentation/latest/reference/drawing.html#module-networkx.drawing.layout 在我从此处的文档中检查了一些信息之后: http : //networkx.github.io/documentation/latest/reference/drawing.html#module-networkx.drawing.layout

I changed the format of layout to 我将布局格式更改为

pos=nx.circular_layout(self.G, scale=5)

and it works! 而且有效! 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM