有向圖中的節點具有Name,Age和Height作為屬性。我想繪制三個屬性的分布,這可能嗎?
我知道可以通過這種方式獲取屬性:
name = nx.get_node_attributes(G, "Name")
age = nx.get_node_attributes(G, "Age")
height = nx.get_node_attributes(G, "Height")
但我真的不明白如何使用它們而不是G下面的函式?
import networkx as nx
def plot_degree_dist(G):
degrees = [G.degree(n) for n in G.nodes()]
plt.hist(degrees)
plt.show()
plot_degree_dist(nx.gnp_random_graph(100, 0.5, directed=True))

或者有沒有更好的方法來繪制節點屬性的分布?
uj5u.com熱心網友回復:
對我來說似乎是一種完全合理的方式。我不知道有什么更方便的方法。為了更通用,向您的函式添加一個引數,該引數采用您要繪制的屬性的名稱。
只知道nx.get_node_attributes()回傳一個以節點為鍵的字典。由于我們只是繪制分布圖,因此我們只對值感興趣,而不對鍵感興趣。
以下是您的引導后的一個獨立示例:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
def plot_attribute_dist(G, attribute):
attribute = nx.get_node_attributes(G, attribute).values()
plt.hist(attribute)
plt.show()
attribute_name = 'Name'
G = nx.gnp_random_graph(100, 0.5, directed=True)
rng = np.random.default_rng(seed=42)
for node, data in G.nodes(data=True):
data[attribute_name] = rng.normal()
plot_attribute_dist(G, attribute_name)
哪個輸出

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/339917.html
標籤:Python matplotlib 网络x
