当我试图将不同的颜色应用到我的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发布于 2020-04-10 23:07:22
在以下几行:
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"].values或EMM["id"].values中时,才为它分配颜色。我想你有两个节点都没有。因此,当您试图绘制它时,节点列表比颜色列表更长。
无论何时使用elif,您都需要考虑到存在这样一种可能性,即有些东西既不满足任何条件,也不满足任何条件。否则,您应该使用else。
https://stackoverflow.com/questions/61141648
复制相似问题