使用NetwrokX/Matplotlib防止边缘和节点重叠 [英] Preventing Overlap of edges and nodes with NetwrokX/Matplotlib

查看:346
本文介绍了使用NetwrokX/Matplotlib防止边缘和节点重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制问题"Collat​​z猜想"的数字,该问题在数字(节点标签)之间形成了一个很好的网络.但是,在使用networkx nx.spring_layout(G)的解决方案的最终图中,我得到了重叠的边和节点:

nx.spring_layout配置为(通过反复试验)为:

 pos=nx.spring_layout(G,k = 0.004, iterations = 500, scale = 0.6)
 nx.draw(G, labels=labels, pos=pos, font_size = 6, alpha = 0.5, node_size = nodes.values())
 plt.show()

鉴于绘图窗口中有足够的空白空间,是否有一种特殊的方法来防止这种重叠?在这种情况下,节点大小由以下值决定:len(G.neighbors(node)) 没有节点具有超过3个邻居,并且最大节点大小上限为300,以进一步防止重叠. 奇怪的是,对于较长的分支,它似乎处理得很好,但是对于图像中的[24,12,6,3,10]之类的分支,它无法充分划分该分支,在其他情况下,完全重叠.请告知我是否应该使用nx.spring_layout(G)以外的其他布局,这样可能会更好地解决此问题.

解决方案

选择DYZ关于使用graphviz_layout()的真正有用的评论,我想我会在这里分享我的经验,以防其他人遇到类似的情况.问题,因为进行此设置并非一帆风顺(至少对我而言,至少在Ubuntu 16.04上如此):

我发现sudo pip install pygraphviz无法正常工作,因为它找不到所需的文件之一.然后,我尝试了sudo apt-get install graphviz libgraphviz-dev pkg-config,然后尝试了sudo pip install pygraphviz(由于此建议), 看上去看起来像它一样有效(即安装成功),但是我仍然无法通过NetworkX使用graphviz_layout().因此,我卸载了pygraphviz(sudo pip uninstall pygraphviz)并按如下方式再次安装(感谢

这项 did 工作,与使用NetworkX的spring_layout()相比,使用graphviz_layout()进行可视化的节点重叠似乎更少,并且在边缘长度和群集间距方面更具美感. /p>

I am trying to plot numbers of a problem "Collatz Conjecture" which forms a nice network between numbers(node labels). However, in the final plot of the solution using networkx nx.spring_layout(G) I get overlapping edges and nodes:

The nx.spring_layout is configured(through trial and error) as:

 pos=nx.spring_layout(G,k = 0.004, iterations = 500, scale = 0.6)
 nx.draw(G, labels=labels, pos=pos, font_size = 6, alpha = 0.5, node_size = nodes.values())
 plt.show()

Is there a particular way to prevent this overlap, given that there is adequate empty space in the plotting window? Node size in this case is dictated by the value of:len(G.neighbors(node)) No node has more that 3 neighbours and the max node size is capped at 300 to further prevent overlap. The odd thing is that for the longer branches this seems to be handled fairly well, but for branches such as [24,12,6,3,10] as in the images, it fails to demarcate this branch sufficiently, in other cases it overlaps altogether. Please advise if I should use another layout besides nx.spring_layout(G) which might handle this better.

解决方案

Picking up on the really helpful comment from DYZ about using graphviz_layout(), I thought I'd share my experience here, in case anyone else is having a similar issue, because getting this set up wasn't totally straightforward (for me, at least, on Ubuntu 16.04):

I found sudo pip install pygraphviz didn't work, because it couldn't find one of the files it needed. I then tried sudo apt-get install graphviz libgraphviz-dev pkg-config followed by sudo pip install pygraphviz (thanks to this suggestion), which looked like it worked (i.e. installation succeeded), but I still couldn't use graphviz_layout() through NetworkX. So, I uninstalled pygraphviz (sudo pip uninstall pygraphviz) and installed it again as follows (thanks to a tip on this thread):

sudo pip install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"

This did work, and visualisations with graphviz_layout() do seem to have less node overlap than using NetworkX's spring_layout(), as well as a more æsthetically pleasing approach to edge length and cluster spacing.

这篇关于使用NetwrokX/Matplotlib防止边缘和节点重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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