0

I am making a large bipartite graph, I am able to make a graph but I cannot label the nodes in the graph, I am interested in simply labeling the nodelistDept nodes with their names I have tried everything but nothing seems to work, any help will be much appreciated, here is the code:

pos=nx.networkx.spring_layout(G)
#draw nodes
nx.draw_networkx_nodes(G, pos, nodelist=nodelistDept,node_color = 'r', node_size = 400)
nx.draw_networkx_nodes(G, pos, nodelist=nodelistSup,node_color= 'w', node_size=50)
#draw edges
nx.draw_networkx_edges(G,pos,width=0.5,alpha=0.5)
nx.draw_networkx_edges(G,pos,edgelist=edgeList)
#draw the labels
#labels = nodelistDept
#nx.draw_networkx_labels(G,pos)

nx.draw_networkx_labels(G,pos, font_size=12,font_family='sans-serif')
plt.show()
dbmehri
  • 21
  • 1
  • 3
  • This is easier to work with if you give a minimal working example. I'd like to just copy and paste your code and run it, but it doesn't know `nx`, `plt`, `nodelistDept`, `nodelistSup`,... So it should have everything defined it needs to run, and things that aren't necessary to see the problem you're seeing should be removed. – Joel Apr 16 '15 at 23:49
  • After rereading your question: are you asking about how to label just a subset of the nodes? To expand on my earlier comment - it's really useful to provide code that car run, tell us what the output is, and what you want the output to be. – Joel Apr 17 '15 at 04:44

3 Answers3

2

I don't know if it is a silly answer, but have u tried for instance:

 nx.draw_spectral(G, with_labels=True) ?

It worked for me.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

I think the error is somewhere else. When I run a modified version of your code as follows:

import networkx as nx
import pylab as plt

G=nx.fast_gnp_random_graph(15,0.1)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos)

plt.savefig('labels.png')

I get

enter image description here

So the labels are there.


edit upon rereading your question, I think you might be asking how to just label a subset of the nodes. Please clarify if that is what you want.

If you're trying to just label a subset of nodes:

subnodelist = [1,2,3,4,5,6,7,8]
labels = {k:k for k in subnodelist}    
plt.clf()
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos, labels)
plt.savefig('labels2.png')

This gives

enter image description here

What I do with labels= {k:k for k in subnodelist} is create a dict with each node label keyed to its name.

Something else that could explain the problem is the use of show.

When I run plt.show() on my macbook in ipython, it fails to display the labels, but that is because of some issue with the rendering - I get a long error message which I've never tried to sort out. When I save the figure it is fine. More detail on the error is here: pylab/networkx; no node labels displayed after update

Community
  • 1
  • 1
Joel
  • 22,598
  • 6
  • 69
  • 93
  • hi joel, thanks much for your suggestions, i am trying to label all nodes, but i think what is happening is that the labels are not showing up in the plot that i am making using plt.show(), but perhaps they are there if i save to a pic file – dbmehri Apr 18 '15 at 04:48
  • Ah - yes. I deleted that from an earlier version of my answer. I'll put it back in. At least with ipython on my mac, if I use plt.show, I get an error in the renderring, but plt.savefig is fine. – Joel Apr 18 '15 at 08:32
  • I tried to do what you suggested but to no avail, still no labels, I am getting a "bad argument type for built-in operation" error, here is my code: labels = {k:k for k in nodelistDept} plt.clf() nx.draw_networkx_labels(G,pos, labels) plt.show() – dbmehri Apr 21 '15 at 04:55
  • I don't know how to sort out the issue with displaying labels using `plot.show()`. The saving works for me [i.e. `plt.savefig('...')`]. It looks like you may have the same error as I do. I don't have a solution for that. See http://stackoverflow.com/questions/11415836/typeerror-bad-argument-type-for-built-in-operation – Joel Apr 21 '15 at 11:01
  • I believe this is the error you're running into: http://stackoverflow.com/questions/25865975/pylab-networkx-no-node-labels-displayed-after-update/25870916 – Joel Apr 23 '15 at 11:47
  • i am working on a graph with several thousand nodes and probably what is happening is that since it is so large, networkx is dumping the labels, either way, there seems to be a bug in the code – dbmehri Apr 23 '15 at 20:11
0

You are missing your "labels" dictionary to pass as parameter in last line of code. Correct code is:

nx.draw_networkx_labels(G,pos, labels=labels font_size=12,font_family='sans-serif')