Sergej Andrejev
Sergej Andrejev

Reputation: 9413

Passing edge weights to graphviz_layout in networkx

everybody cannot find how to pass name of property of list of weights to graphviz_layout in networkx. Something like this:

nx.spring_layout(G, weight='weight.sum')

but with nx.graphviz_layout(G, ...). Maybe somebody will know?

Upvotes: 5

Views: 2495

Answers (1)

Maehler
Maehler

Reputation: 6331

If I got you right, you want to specify which edge attribute to use as edge weight for the graphviz layout. From the NetworkX docs I don't see that this is possible.

However, the weight attribute is used by the layout algorithms in graphviz. This is what it says:

weight

Weight of edge. In dot, the heavier the weight, the shorter, straighter and more vertical the edge is. For other layouts, a larger weight encourages the layout to make the edge length closer to that specified by the len attribute.

With this you can set the edge weight for each edge in the network:

g = nx.Graph()
g.add_edge(1, 2, {'weight': 4})

or

g.add_edge(1, 2)
g.edge[1][2]['weight'] = 4

or

g[1][2]['weight'] = 4

If you want to set a default weight for the edges, you can pass this as an argument to the graphviz program that you want to use in graphviz_layout:

nx.graphviz_layout(g, prog='dot', args='-Eweight=4')

Upvotes: 7

Related Questions