2

I'm using Python to conduct social network analysis, very simple kind, and as a newbie (to both SNA and Python).

When drawing a graph using Terminal on my mac, I've tried every method I can but still can only draw nodes and edges, but no label of nodes in or beside them.

What scripts should I use to make the labels visible?

>>> import networkx as nx
>>> import networkx.generators.small as gs
>>> import matplotlib.pyplot as plt
>>> g = gs.krackhardt_kite_graph()
>>> nx.draw(g)
>>> plt.show()
Kathryn
  • 123
  • 2
  • 10
  • You might want to have a look at http://stackoverflow.com/questions/29738288/displaying-networkx-graph-with-labels/29820843#29820843 also – A_A Apr 27 '15 at 22:38

2 Answers2

5

EdChum gave a good answer. Another option which will by default not show the axes and produces a graph that takes up slightly more of the figure is to use nx.draw but give it the argument with_labels = True. (for nx.draw, you need to set with_labels to True, but for nx.draw_networkx it defaults to True).

import networkx as nx
import networkx.generators.small as gs
import matplotlib.pyplot as plt
g = gs.krackhardt_kite_graph()
nx.draw(g,with_labels=True)
plt.savefig('tmp.png')

enter image description here

Be aware that there is a bug such that sometimes plt.show() will not show the labels. From what I've been able to tell, it's not in networkx, but rather has something to do with the rendering. It saves fine, so I haven't worried about following up on it in detail. It shows up for me using ipython on a macbook. Not sure what other systems it's on. More detail at pylab/networkx; no node labels displayed after update

Community
  • 1
  • 1
Joel
  • 22,598
  • 6
  • 69
  • 93
  • That's cool, Joel!! When I saved the image to a file, using this line you provided (plt.savefig('tmp.png')), the label appears. I'm also using mac, and don't know why "plt.show()" can't call out the labels. – Kathryn Apr 23 '15 at 12:28
  • I tried to "accept" or "like" your answer, but it seems I can't do it because I don't have enough reputation. How should I accept your answer? – Kathryn Apr 23 '15 at 12:32
  • The problem for why `plt.show()` isn't working is to do with a (known) bug in matplotlib. I'm not sure if they've fixed it in newer versions or not. By the way - you can also save as .pdf or several other formats just by using the appropriate extension like `plt.savefig('name.pdf')` – Joel Apr 23 '15 at 12:58
  • You can't vote answers up until you have higher reputation, but there should be a checkmark at about the same place as the up/down arrows where you can click to accept. – Joel Apr 23 '15 at 12:59
  • Ah yes!! I found that! : ) – Kathryn Apr 25 '15 at 02:48
  • No, I don't think they have fixed the bug, because I have the newest version of matplotlib installed just early this week, and there the bug still exists. – Kathryn Apr 25 '15 at 02:50
3

Try using draw_networkx:

import networkx as nx
import networkx.generators.small as gs
import matplotlib.pyplot as plt
g = gs.krackhardt_kite_graph()
nx.draw_networkx(g)
plt.show()

This results in:

enter image description here

with_labels is by default True so not necessary to specify

EdChum
  • 376,765
  • 198
  • 813
  • 562