根据列表更改特定节点形状
Change specific nodes shapes based on a list
由于可以更改图中特定节点的节点颜色、边颜色、节点大小等,我想知道是否有办法使用列表更改特定节点的形状。我试过以下代码:
node_shape = []
for node in S:
if node in nodes_of_interest:
node_shape.append('d')
else:
node_shape.append('o')
nx.draw_networkx(S, pos = layout, font_size = '14', node_color = color_map, edgecolors = edge_colors, node_size = node_size,font_weight='bold', )
但是我收到以下错误:
ValueError: Unrecognized marker style ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'd', 'o', 'o', 'd', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'd', 'o', 'o', 'o', 'o', 'o', 'd', 'd', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'd', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
我想知道我是否犯了错误,或者这是不可能的。提前感谢您的所有建议!
节点最终使用matplotlib的scatter
. This is from the source code for nx.draw_network_nodes
, which is called from within nx.draw_networkx
:
绘制
node_collection = ax.scatter(xy[:, 0], xy[:, 1],
s=node_size,
c=node_color,
marker=node_shape,
cmap=cmap,
vmin=vmin,
vmax=vmax,
alpha=alpha,
linewidths=linewidths,
edgecolors=edgecolors,
label=label)
但是每次调用 scatter
将只接受一个标记用于绘制所有点。因此,为了拥有不同的标记,您需要多次调用绘图函数——针对具有相同标记样式的每个节点子集调用一次。这是一个简单的工作示例:
G = nx.Graph(np.array([[0,1,0],[1,0,1],[0,1,0]]))
layout = nx.layout.spring_layout(G)
nodes_of_interest = [0,2]
other_nodes = [1]
nx.draw_networkx(G,layout,nodelist=nodes_of_interest,node_shape='d')
nx.draw_networkx(G,layout,nodelist=other_nodes,node_shape='o')
PS:为了进行微调,通常直接将 nx.draw_network_nodes
与 nx.draw_network_edges
一起使用会很有帮助,而不是 nx.draw_networkx
.
由于可以更改图中特定节点的节点颜色、边颜色、节点大小等,我想知道是否有办法使用列表更改特定节点的形状。我试过以下代码:
node_shape = []
for node in S:
if node in nodes_of_interest:
node_shape.append('d')
else:
node_shape.append('o')
nx.draw_networkx(S, pos = layout, font_size = '14', node_color = color_map, edgecolors = edge_colors, node_size = node_size,font_weight='bold', )
但是我收到以下错误:
ValueError: Unrecognized marker style ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'd', 'o', 'o', 'd', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'd', 'o', 'o', 'o', 'o', 'o', 'd', 'd', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'd', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
我想知道我是否犯了错误,或者这是不可能的。提前感谢您的所有建议!
节点最终使用matplotlib的scatter
. This is from the source code for nx.draw_network_nodes
, which is called from within nx.draw_networkx
:
node_collection = ax.scatter(xy[:, 0], xy[:, 1],
s=node_size,
c=node_color,
marker=node_shape,
cmap=cmap,
vmin=vmin,
vmax=vmax,
alpha=alpha,
linewidths=linewidths,
edgecolors=edgecolors,
label=label)
但是每次调用 scatter
将只接受一个标记用于绘制所有点。因此,为了拥有不同的标记,您需要多次调用绘图函数——针对具有相同标记样式的每个节点子集调用一次。这是一个简单的工作示例:
G = nx.Graph(np.array([[0,1,0],[1,0,1],[0,1,0]]))
layout = nx.layout.spring_layout(G)
nodes_of_interest = [0,2]
other_nodes = [1]
nx.draw_networkx(G,layout,nodelist=nodes_of_interest,node_shape='d')
nx.draw_networkx(G,layout,nodelist=other_nodes,node_shape='o')
PS:为了进行微调,通常直接将 nx.draw_network_nodes
与 nx.draw_network_edges
一起使用会很有帮助,而不是 nx.draw_networkx
.