1

I am adding labels with

G.add_edge(node1, node2, label=label)

drawing with

pos = nx.spring_layout(G)

nx.draw(G, pos)
nx.draw_networkx_edge_labels(G, pos)

and getting

enter image description here

which I don't want. I don't want label look like {'label': 'a"} I want it be just a.

Is it possible? I saw examples, where labels were passing into draw function, which is not good for me. I want labels to be stored in the graph and displayed later.

Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

2

You can use the edge_labels argument to specify what to draw. Example:

>>> import networkx as nx
>>> G = nx.Graph()
>>> G.add_edge(1, 2, label='a')
>>> pos = nx.spring_layout(G)                                                    
>>> nx.draw(G, pos)                                                              
>>> nx.draw_networkx_edge_labels(G,pos,edge_labels=nx.get_edge_attributes(G,'label'))
{(1, 2): Text(0,0,'a')}
abc
  • 11,579
  • 2
  • 26
  • 51
  • how to offset the "a" above perpendicular to the arrow? so that it doesn't lie "on" the arrow, but slightly above it? – user8395964 Apr 19 '23 at 08:12