Mike
Mike

Reputation: 45

NetworkX cmap not having any effect on node colour

I am trying to visualise communities in a node network using networkx, the code is working fine in terms of detecting the communities however it is in greyscale. I have been unable to get the cmap parameter to have any effect on the output network when used in conjunction with nodelist and node_color parameters.

Current output: https://i.sstatic.net/Nq8T3.png

import networkx as nx
import matplotlib.pyplot as plt
import community

g=nx.read_edgelist('communities.txt',create_using=nx.Graph(),nodetype=int)

partition = community.best_partition(g)

print nx.info(g)

size = float(len(set(partition.values())))
print "Communities: " + str(size)

sp=nx.spring_layout(g)
plt.axis('off')

count = 0
for group in set(partition.values()) :
    count += 1
    list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == group]
    nx.draw_networkx_nodes(g, pos=sp,
        nodelist=list_nodes,
        with_labels=False,
        node_size=30,
        node_color = str(count/size),
        cmap=plt.get_cmap('RdYlBu'))


nx.draw_networkx_edges(g, sp, alpha=0.3)
plt.show(g)

Upvotes: 0

Views: 757

Answers (1)

DYZ
DYZ

Reputation: 57033

For the cmap option to work, node_color must be a list of floating-point numbers, not a string. Also, vmin and vmax options must be provided. The numeric values are mapped to colors using the cmap and vmin and vmax parameters. (See help(nx.draw_networkx_nodes).)

Upvotes: 1

Related Questions