labels with colors

1,784 views
Skip to first unread message

Bob

unread,
Jan 12, 2011, 6:04:37 PM1/12/11
to networkx-discuss
Hi All,
I'd like to color the nodes according to a vector of size n, whose i-
th element is the color of node i. I passed a list to function
nx.draw_networkx_labels but I get an error:


AttributeError: 'list' object has no attribute 'items'

Does anybody know how to fix this?

Thanks

Richard Careaga

unread,
Jan 12, 2011, 6:13:58 PM1/12/11
to networkx...@googlegroups.com
You want

networkx.draw_networkx_nodes


http://goo.gl/pByha



Bob
January 12, 2011 6:04 PM

Bob

unread,
Jan 12, 2011, 7:28:29 PM1/12/11
to networkx-discuss
I can only change nodes color with that function, not their labels I
think

On Jan 12, 3:13 pm, Richard Careaga <leuc...@gmail.com> wrote:
> You want
>
>   networkx.draw_networkx_nodes�
>   <http://networkx.lanl.gov/reference/generated/networkx.draw_networkx_n...>
>
> http://goo.gl/pByha
>
>
>
>
>
>
>
> > ------------------------------------------------------------------------
>
> >    Bob <mailto:roberto.pagli...@gmail.com>

Dan Schult

unread,
Jan 12, 2011, 8:13:46 PM1/12/11
to networkx...@googlegroups.com
It looks like you need to use a dict keyed
by node to the label instead of a list.
The attribute "items" is a dict method.
It returns the (key,value) pairs for the dict.

One easy way to turn your list into a dict (since the key
will be the index of the element) is dict(enumerate(yourlist))
Dan

> --
> You received this message because you are subscribed to the Google
> Groups "networkx-discuss" group.
> To post to this group, send email to networkx-
> dis...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discuss
> +unsub...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/
> group/networkx-discuss?hl=en.
>

Aric Hagberg

unread,
Jan 12, 2011, 8:39:21 PM1/12/11
to networkx...@googlegroups.com

You have to add the labels individual if you want separate colors (not
an optimal interface).
e.g.


import networkx as nx
import matplotlib.pyplot as plt

# draw graph without labels
G=nx.path_graph(4)
pos=nx.spring_layout(G)
nx.draw_networkx(G,pos,node_color='w',with_labels=False)

# set colors
colors=['r','g','b','c']

# draw node labels one-at-a-time with color
for node in G:
color=colors[node]
nx.draw_networkx_labels(G,pos,labels={node:node},font_color=color)

plt.show()

Aric

Reply all
Reply to author
Forward
0 new messages