如何用不同大小的节点绘制图形

时间:2020-03-09 09:08:11

标签: python networkx

我用节点之间链接的权重计算了节点的度中心性。 下一个任务是绘制一个具有不同大小的节点的图形。例如,如果中心度> 4,则节点大小= 1500,如果<4 = 500。 帮助了解错误在哪里。

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

A = [[0, 1.51, 0, 1.71, 0],
     [0, 0, 2.11, 1.81, 2.31],
     [0, 0, 0, 1.31, 1.41],
     [0, 0, 0, 0, 1.11],
     [0, 0, 0, 0, 0]]

G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)

layout = nx.spring_layout(G)
labels = nx.get_edge_attributes(G, "weight")


# a list of the node labels in the right order
raw_labels = ["A1", "K2", "D3", "E4", "Z30"]
lab_node = dict(zip(G.nodes, raw_labels))

print("Degree centrality weight")
d = G.degree(weight='weight')
print(d)

for x in d:
    if x[1] > 4:
        large = x
        print (large)
    else:
        small = x
        print (small)

nx.draw(G, layout)
nx.draw_networkx_nodes(G, layout, edgelist=large, node_size=100)
nx.draw_networkx_nodes(G, layout, edgelist=small, node_size=1500)
nx.draw_networkx_edge_labels(G, layout, edge_labels=labels)
nx.draw_networkx_labels(G, layout, labels=lab_node, font_size=10, font_family='sans-serif')
plt.show()

1 个答案:

答案 0 :(得分:0)

以下代码有效。在您的代码中存在一些问题:首先,就像乔尔在注释中提出的那样,您使用了using(WebClient webclient = new WebClient()) { webclient.Encoding = UTF8Encoding.UTF8; webclient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; webclient.UseDefaultCredentials = true; blankString=""; webclient.UploadString(url, blankString); webclient.UploadString(url, stringToUpload); } small作为变量,但希望将它们列出。其次,您在draw_networkx_nodes中使用了large而不是edgelist。 我将nodelist替换为nx.draw(并添加了nx.draw_networkx_edges),以允许其他用户绘制的节点尺寸小于或小于默认尺寸,因为较小的尺寸不适用于plt.axis("off")

作为最后的个人建议,我将用更长的自我解释名称(例如nx.draw,{{)替换变量名称(例如dGsmall) 1}},node_degrees

graph