1 回答

TA貢獻1886條經驗 獲得超2個贊
不幸的nx.draw_networkx_nodes
是不接受可迭代的形狀,因此您必須遍歷節點并單獨繪制它們。此外,我們必須索引生成的cmap
,否則,單值社區值將映射到相同的初始 cmap 顏色。對于可能的形狀,我只是復制文檔中提到的可用形狀的字符串,并根據分區號對其進行索引:
# load the karate club graph
G = nx.karate_club_graph()
# compute the best partition
partition = community_louvain.best_partition(G)
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
shapes = 'so^>v<dph8'
plt.figure(figsize=(12,8))
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_edges(G, pos, alpha=0.5)
for node, color in partition.items():
nx.draw_networkx_nodes(G, pos, [node], node_size=100,
node_color=[cmap.colors[color]],
node_shape=shapes[color])
添加回答
舉報