I am getting the following errors when I am trying to apply different colors to the different node-sets or Ks in my k-partite graph.
G = nx.Graph()
G.add_nodes_from(emc["entity"], bipartite=0)
G.add_nodes_from(set(EMM_unique["keys"]).symmetric_difference(set(emc["entity"])), bipartite=1)
G.add_nodes_from(EMM["id"], bipartite=2)
G.add_edges_from(list(emc.itertuples(index=False)), color='red')
G.add_edges_from(list(EMM.itertuples(index=False)))
nodes = G.nodes()
# for each of the parts create a set
nodes_0 = set([n for n in nodes if G.nodes[n]['bipartite']==0])
nodes_1 = set([n for n in nodes if G.nodes[n]['bipartite']==1])
nodes_2 = set([n for n in nodes if G.nodes[n]['bipartite']==2])
# set the location of the nodes for each set
pos = dict()
pos.update( (n, (1, i)) for i, n in enumerate(nodes_0) ) # put nodes from X at x=1
pos.update( (n, (2, i)) for i, n in enumerate(nodes_1) ) # put nodes from Y at x=2
pos.update( (n, (3, i)) for i, n in enumerate(nodes_2) ) # put nodes from X at x=1
# To apply colors to different node-sets
color_map = []
for node in G:
if node in emc["entity"].values:
print(node)
color_map.append('red')
elif node in EMM["id"].values:
color_map.append('green')
#nx.draw_networkx_nodes(G,pos, node_color=color_map)
nx.draw(G, pos, node_color=color_map, width= 2, with_labels=True, with_arrows=True)
plt.show()
If I don't use color_map code then it runs fine. With color_map code I am getting the following error message
Traceback (most recent call last):
File "C:\Python\lib\site-packages\matplotlib\axes\_axes.py", line 4291, in _parse_scatter_color_args
raise ValueError
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Pawandeep/Desktop/CVS1/dev/AnnotationBexis.py", line 287, in <module>
nx.draw(G, pos, node_color=color_map, width= 2, with_labels=True, with_arrows=True)
File "C:\Python\lib\site-packages\networkx\drawing\nx_pylab.py", line 128, in draw
draw_networkx(G, pos=pos, ax=ax, **kwds)
File "C:\Python\lib\site-packages\networkx\drawing\nx_pylab.py", line 279, in draw_networkx
node_collection = draw_networkx_nodes(G, pos, **kwds)
File "C:\Python\lib\site-packages\networkx\drawing\nx_pylab.py", line 416, in draw_networkx_nodes
label=label)
File "C:\Python\lib\site-packages\matplotlib\__init__.py", line 1601, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Python\lib\site-packages\matplotlib\axes\_axes.py", line 4454, in scatter
get_next_color_func=self._get_patches_for_fill.get_next_color)
File "C:\Python\lib\site-packages\matplotlib\axes\_axes.py", line 4298, in _parse_scatter_color_args
.format(nc=n_elem, xs=xsize, ys=ysize)
ValueError: 'c' argument has 28 elements, which is not acceptable for use with 'x' with size 33, 'y' with size 33.
I have already tried adding the nodes separately with the color through this code but getting the similar error
nx.draw_networkx_nodes(G,pos, node_color=color_map)
My requirement is to have different colors for different node-set. It would be very helpful if I can get some assistance with that.
Solution:
As per the suggestion from the accepted answer, I have added condition for all three node sets so now
for node in G:
if node in emc["entity"].values:
color_map.append("red")
elif node in EMM["id"].values:
color_map.append("green")
else:
color_map.append("blue") # solution
In these lines:
color_map = []
for node in G:
if node in emc["entity"].values:
print(node)
color_map.append('red')
elif node in EMM["id"].values:
color_map.append('green')
you assign a color to a node only if it is in emc["entity"].values
or EMM["id"].values
. I think you have some nodes that are in neither. So when you try to draw it, the list of nodes is longer than the list of colors.
Anytime you use an elif
you need to allow for the possibility that there are some things that satisfy neither condition. Otherwise you should use else
.