2

This is my code in Python 2.7.9:

import matplotlib.pyplot as plt
import networkx as nx
socialNetworl = nx.Graph()
socialNetworl.add_nodes_from([1,2,3,4,5,6])
socialNetworl.add_edges_from([(1,2),(1,3),(2,3),(2,5),(2,6)]) 
nx.draw(socialNetworl, node_size = 800, node_color="cyan")
plt.show()

But I can't see the node labels. Is there a line of code I forgot?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
GSandro_Strongs
  • 773
  • 3
  • 11
  • 24
  • possible duplicate of [Plotting networkx graph with node labels defaulting to node name](http://stackoverflow.com/questions/28533111/plotting-networkx-graph-with-node-labels-defaulting-to-node-name) – Joel Mar 03 '15 at 02:10

2 Answers2

5

Just add with_labels=True to your code.

import matplotlib.pyplot as plt
import networkx as nx
socialNetworl = nx.Graph()
socialNetworl.add_nodes_from([1,2,3,4,5,6])
socialNetworl.add_edges_from([(1,2),(1,3),(2,3),(2,5),(2,6)]) 
nx.draw(socialNetworl, node_size = 800, node_color="cyan", with_labels = True)
plt.show()

enter image description here

If you want to change the labels, create a dict with the labels and pass labels=theLabelDict into nx.draw:

import matplotlib.pyplot as plt
import networkx as nx
socialNetworl = nx.Graph()
socialNetworl.add_nodes_from([1,2,3,4,5,6])
socialNetworl.add_edges_from([(1,2),(1,3),(2,3),(2,5),(2,6)]) 
labels = {1:'King Arthur', 2:'Lancelot', 3:'shopkeeper', 4:'dead parrot', 5:'Brian', 6:'Sir Robin'}
nx.draw(socialNetworl, node_size = 800, node_color="cyan", labels=labels, with_labels = True)
plt.show()

enter image description here

Joel
  • 22,598
  • 6
  • 69
  • 93
  • I have a problem, In my case I get names from a database, the error occurs when there is a name that contains letter Ñ for example a last name BOLAÑOS, I used # -*- coding: utf8 -*- but isn't enough. I read that matplotlib needs a file which should contain special characters. Thanks in advance. – GSandro_Strongs Mar 04 '15 at 19:39
3

You can draw the node labels separately with nx.draw_networkx_labels (and control lots of other label options too). For example, after adding the nodes and edges, you could write:

pos=nx.spring_layout(socialNetworl)
nx.draw(socialNetworl, pos=pos, node_size = 800, node_color="cyan")
nx.draw_networkx_labels(socialNetworl, pos=pos);
plt.show()

Which draws:

enter image description here

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
  • I'm doing a social network diagram, I tried to change numbers label by names labels but I got errors. Is posible to change numbers to first names and last names? for example 5 to Fred Kennedy? – GSandro_Strongs Mar 02 '15 at 23:14
  • 1
    @ajcr You can simplify your code. No need for the `nx.draw_networkx_labels` call. Just do `nx.draw` and pass it `with_labels=True` – Joel Mar 03 '15 at 02:12
  • 1
    @gs_developer_user3605534 My answer at http://stackoverflow.com/questions/28533111/plotting-networkx-graph-with-node-labels-defaulting-to-node-name/28533293#28533293 gives the way to change what label appears. Note, you can do this all in just the `nx.draw`, rather than adding the `nx.draw_networkx_labels` – Joel Mar 03 '15 at 02:13
  • Thanks @Joel - with some recent-ish work I'd got myself in the habit of separating the two and drawing the labels separately with `draw_networkx_labels`. For cases like the OP's I'd certainly opt for your answer. – Alex Riley Mar 03 '15 at 09:24
  • I'd ask another question, In my diagram I have 600 nodes but when it appears the nodes are too joined, how can I show less joined? – GSandro_Strongs Mar 03 '15 at 20:21