"ValueError: 'c' argument has 28 elements, which is not acceptable for use with 'x' with size 33, 'y' with size 33" with Networkx k-partite graph

"ValueError: 'c' argument has 28 elements, which is not acceptable for use with 'x' with size 33, 'y' with size 33" with Networkx k-partite graph

当我尝试将不同的颜色应用于我的 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:\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.

我已经尝试通过此代码分别添加带有颜色的节点,但得到了类似的错误

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