"ValueError: 'c' argument has 28 elements, which is not acceptable for use with 'x' with size 3



当我试图将不同的颜色应用于k部分图中的不同节点集或Ks时,会出现以下错误。

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()

如果我不使用color_map代码,那么它运行得很好。使用color_map代码,我收到以下错误消息

Traceback (most recent call last):
File "C:Pythonlibsite-packagesmatplotlibaxes_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 FilesJetBrainsPyCharm 2019.2.5helperspydev_pydev_bundlepydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
File "C:Program FilesJetBrainsPyCharm 2019.2.5helperspydev_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:Pythonlibsite-packagesnetworkxdrawingnx_pylab.py", line 128, in draw
draw_networkx(G, pos=pos, ax=ax, **kwds)
File "C:Pythonlibsite-packagesnetworkxdrawingnx_pylab.py", line 279, in draw_networkx
node_collection = draw_networkx_nodes(G, pos, **kwds)
File "C:Pythonlibsite-packagesnetworkxdrawingnx_pylab.py", line 416, in draw_networkx_nodes
label=label)
File "C:Pythonlibsite-packagesmatplotlib__init__.py", line 1601, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:Pythonlibsite-packagesmatplotlibaxes_axes.py", line 4454, in scatter
get_next_color_func=self._get_patches_for_fill.get_next_color)
File "C:Pythonlibsite-packagesmatplotlibaxes_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.

我已经尝试通过这个代码单独添加带有颜色的节点,但得到了类似的错误

nx.draw_networkx_nodes(G,pos, node_color=color_map)

我的要求是为不同的节点集提供不同的颜色。如果我能在这方面得到一些帮助,那将是非常有帮助的。

解决方案:

根据已接受答案的建议,我已经为所有三个节点集添加了条件,所以现在

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

在这些行中:

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')

仅当节点处于emc["entity"].valuesEMM["id"].values时,才能为其指定颜色。我认为您有一些节点都不在。因此,当您尝试绘制它时,节点列表比颜色列表更长。

任何时候使用elif,都需要考虑到某些东西既不满足这两个条件的可能性。否则,应使用else

最新更新