2

If I make a tree using networkx, by default the nodes have integer ids. I would like to visualise the tree with the integers labelling the nodes. So instead of:

import matplotlib.pyplot as plt
import networkx as nx
T = nx.generators.balanced_tree(2, 2)
nx.draw(T)
plt.show()

enter image description here

How can I write the integer labels inside the nodes?

Simd
  • 19,447
  • 42
  • 136
  • 271

1 Answers1

2

Instead of nx.draw, use nx.draw_networkx. It has a keyword argument with_labels that is True by default, so you don't really have to change anything except the function.

import matplotlib.pyplot as plt
import networkx as nx
T = nx.generators.balanced_tree(2, 2)
nx.draw_networkx(T)
plt.show()

enter image description here

brentertainer
  • 2,118
  • 1
  • 6
  • 15
  • Or just `nx.draw(with_labels=True)` which is arguably cleaner as it doesn't also change other defaults. – ijoseph Oct 27 '20 at 00:08