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

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

如何根據我的成員向量用不同的標簽繪制我的圖表?

如何根據我的成員向量用不同的標簽繪制我的圖表?

qq_遁去的一_1 2022-01-05 19:42:01
我有一張圖,經過一些分類后,我提取了一個像這樣的隸屬向量:vector membership is : [1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 3, 3, 3, 3]這意味著它的節點號0:4在同一類中?,F在我需要繪制一個圖表,其中具有相同編號的節點應該具有相同的標簽。這里的標簽數量是 3。最簡單的方法是什么?
查看完整描述

1 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

首先,您需要創建一個 Graph,其中有一個包含該節點標簽的屬性


import matplotlib.pyplot as plt

import networkx as nx


member_labels = [1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 3, 3, 3, 3]

node_ids = list(range(len(member_labels)))


node_label_dict = dict(zip(node_ids, member_labels))


G = nx.Graph()

G.add_nodes_from(node_ids)

nx.set_node_attributes(G, node_label_dict, 'label')

所以現在您的 Graph 具有以下節點和屬性


print(G.nodes(data=True))

#NodeDataView({0: {'label': 1}, 1: {'label': 1}, 2: {'label': 1}, 3: {'label': 1}, 4: {'label': 1}, 5: {'label': 2}, 6: {'label': 1}, 7: {'label': 2}, 8: {'label': 2}, 9: {'label': 2}, 10: {'label': 3}, 11: {'label': 3}, 12: {'label': 3}, 13: {'label': 3}})

現在你只需要繪制節點和標簽


pos=nx.nx.spring_layout(G)

nx.nx.draw_networkx_nodes(G, pos)

更新


# Fixed the variable name

labels = nx.nx.draw_networkx_labels(G,pos, labels=node_label_dict)

http://img1.sycdn.imooc.com//61d584280001f37d03870250.jpg

查看完整回答
反對 回復 2022-01-05
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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