亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Networkx 中 Louvain 分區的可視化

Networkx 中 Louvain 分區的可視化

慕標5832272 2023-03-08 16:04:29
請幫助我更改可視化 Louvain 聚類算法的結果。我從網站 https://github.com/taynaud/python-louvain獲取代碼 我可以重寫代碼以便每個集群都有自己的形狀(圓形、三角形、正方形……)嗎?import community as community_louvainimport matplotlib.cm as cmimport matplotlib.pyplot as pltimport networkx as nx# load the karate club graphG = nx.karate_club_graph()# compute the best partitionpartition = community_louvain.best_partition(G)# draw the graphpos = nx.spring_layout(G)# color the nodes according to their partitioncmap = cm.get_cmap('viridis', max(partition.values()) + 1)nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,                       cmap=cmap, node_color=list(partition.values()))nx.draw_networkx_edges(G, pos, alpha=0.5)plt.show()
查看完整描述

1 回答

?
MM們

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

http://img1.sycdn.imooc.com//640841cb0001fe6d06720441.jpg

查看完整回答
反對 回復 2023-03-08
  • 1 回答
  • 0 關注
  • 303 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號