hello_world
hello_world

Reputation: 31

How to get position of edge weights in a networkx graph?

Currently there is a function in networkx library for getting positions of all nodes: spring_layout. Quoting from the docs, it returns:

dict : A dictionary of positions keyed by node

And can be used as:

G=nx.path_graph(4)
pos = nx.spring_layout(G)

I would like something similar to access the position of an edge-weight for a weighted graph. It should return the position of where the number for edge-weight would be placed, preferably at the center of the edge and just above the edge. (By above, I mean "outside" the graph, so for a horizontally-placed square graph's bottom-most edge, it would be just below the edge).

So the question is, is there anything in-built similar to spring_layout for achieving this? And if not, how to go about it yourself?

Upvotes: 1

Views: 1720

Answers (1)

Gambit1614
Gambit1614

Reputation: 8811

You can use nx.draw_edge_labels which returns a dictionary with edges as keys and (x, y, label) as values

import matplotlib.pyplot as plt
import networkx as nx

# Create a graph
G = nx.path_graph(10)

# Add 2 egdes with labels
G.add_edge(0, 8, name='n1')
G.add_edge(2, 7, name='n2')

# Get the layout
pos = nx.spring_layout(G)

# Draw the graph
nx.draw(G, pos=pos)

# Draw the edge labels
edge_labels = nx.draw_networkx_edge_labels(G, pos)

enter image description here.

Now you can see the variables edge_labels

print(edge_labels)
# {(0, 1): Text(0.436919941201627, -0.2110471432994752, '{}'),
#  (0, 8): Text(0.56941037628304, 0.08059107891826373, "{'name': 'n1'}"),
#  (1, 2): Text(0.12712625526483384, -0.2901338796021985, '{}'),
#  (2, 3): Text(-0.28017240645783603, -0.2947104829441387, '{}'),
#  (2, 7): Text(0.007024254096114596, -0.029867791669433513, "{'name': 'n2'}"),
#  (3, 4): Text(-0.6680363649371021, -0.26708812849092933, '{}'),
#  (4, 5): Text(-0.8016944207643129, -0.0029986274715349814, '{}'),
#  (5, 6): Text(-0.5673817462107436, 0.23808073918504968, '{}'),
#  (6, 7): Text(-0.1465270298295821, 0.23883392944036055, '{}'),
#  (7, 8): Text(0.33035539545007536, 0.2070939421162053, '{}'),
#  (8, 9): Text(0.7914739158501038, 0.2699223242747882, '{}')}

Now to get the position of say, edge (2,7), you just need to do

print(edge_labels[(2,7)].get_position())
# Output: (0.007024254096114596, -0.029867791669433513)

You can read more about the documentation here.

If you want to extract the x,y coordinates of all the edges, you can try this:

edge_label_pos = { k: v.get_position()
                  for k, v in edge_labels.items()}
#{(0, 1): (0.436919941201627, -0.2110471432994752),
# (0, 8): (0.56941037628304, 0.08059107891826373),
# (1, 2): (0.12712625526483384, -0.2901338796021985),
# (2, 3): (-0.28017240645783603, -0.2947104829441387),
# (2, 7): (0.007024254096114596, -0.029867791669433513),
# (3, 4): (-0.6680363649371021, -0.26708812849092933),
# (4, 5): (-0.8016944207643129, -0.0029986274715349814),
# (5, 6): (-0.5673817462107436, 0.23808073918504968),
# (6, 7): (-0.1465270298295821, 0.23883392944036055),
# (7, 8): (0.33035539545007536, 0.2070939421162053),
# (8, 9): (0.7914739158501038, 0.2699223242747882)}

Upvotes: 1

Related Questions