根据graphviz_layout中的权重设置边缘长度 [英] Set edge lengths according to weights in graphviz_layout

查看:288
本文介绍了根据graphviz_layout中的权重设置边缘长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用networkx绘制加权图.我正在使用graphviz_layout生成顶点的位置.我希望图中的边缘长度与我使用的边缘权重成正比.以下是我的代码段以实现此目的:

I'm trying to use networkx to plot a weighted graph. I'm generating the positions of vertices using graphviz_layout. I want the edge lengths in the plot to be proportional to the edge weights I am using. Following is my code snippet to achieve this:

import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout

G = nx.Graph()
G.add_weighted_edges_from(edgelist)
pos = graphviz_layout(G)
nx.draw_networkx(G, pos=pos)

这似乎并不影响图中的边长.我看过这篇文章,但是它仅描述了如何通过将所有边缘的单个默认权重设置为graphviz_layout(或者我还不了解解决方案).

This does not seem to impact edge lengths in the plot. I have seen this post however it only describes how to pass a single default weight for all edges to graphviz_layout (or perhaps I haven't understood the solution).

如何更改代码段以绘制长度与边缘权重成比例的图形?

How may I change my snippet to plot a graph with lengths proportional to edge weights?

在上面的代码段中,edgelist是此格式的列表的列表:

In the code snippet above, edgelist is a list of lists of this format:

edgelist = [[4, 219, 1.414], [4, 7, 3.86]]

推荐答案

关键是设置边缘的len属性,如

The key is to set the len attribute of the edges, as described in the Graphviz docs (and available for Neato). Note that this is a preferred edge length, so you will not have complete control. For example, you can add a single weighted edge (u, v, w) to a graph G like so:

G.add_edge(u, v, len=w)

这是使用为说明使用边缘长度时布局的差异,这是将一级节点("Ginori"和"Pazzi")的两个边缘设置得更长的结果:

To illustrate the difference in layouts when using edge lengths, here is the result where I set two edges of degree one nodes ("Ginori" and "Pazzi") to be longer:

这是 不使用任何边长的结果:

And here is the result not using any edge lengths:

这篇关于根据graphviz_layout中的权重设置边缘长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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