29

Im using networkx for visualization. I see when I use the function draw_networkx_edge_labels I can retrieve the labels for edges.

I want to print the attribute on node ( instead of the label).. try everything almost . still stuck. If i have 5 attributes per node, is there anyway I can print a specific attribute on each node ? For example, if a car node has attributes: size, price, company, .. I want to print the size of the car on each node ?

Don't know whether can output this on graph.

SDR
  • 301
  • 1
  • 3
  • 7
  • if you want to draw with pygraphviz instead see this: https://stackoverflow.com/a/67442702/1601580 since drawing with nx is not recommended that might be better (see networkx docs). – Charlie Parker May 07 '21 at 23:33

1 Answers1

43

You can do it by specifying the labels= keyword

import pylab
import networkx as nx
G=nx.Graph()
G.add_node('Golf',size='small')
G.add_node('Hummer',size='huge')
G.add_edge('Golf','Hummer')
labels = nx.get_node_attributes(G, 'size') 
nx.draw(G,labels=labels,node_size=1000)
pylab.show()
NavaneethaKrishnan
  • 1,213
  • 1
  • 9
  • 22
Aric
  • 24,511
  • 5
  • 78
  • 77
  • 13
    A somewhat easier replacement for line 7: `labels = nx.get_node_attributes(G, 'size')` That function returns a dict like the one created in line 7. – TrentP Jan 02 '17 at 22:38
  • 3
    how about if i want to draw all attributes of all nodes in the drawing? – Luk Aron Nov 26 '20 at 09:40
  • code suggested gives me this error: `networkx.exception.NetworkXError: random_state_index is incorrect`, what does it give you? – Charlie Parker May 07 '21 at 22:29