[英]How to draw a graph with different sized nodes Networkx
I calculated degree centrality for the nodes with the weight of the links between the nodes.我用节点之间链接的权重计算了节点的度中心性。 The next task is to draw a graph with nodes of different sizes.下一个任务是绘制具有不同大小节点的图形。 For example, if the degree centrality > 4, the node size = 1500, if < 4 = 500. Help understand where the error is.比如度中心度>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()
The following code works.以下代码有效。 In your code were some issues: first like already Joel raised in the comments, you used small
and large
as variables, but wanted them to be list.在您的代码中存在一些问题:首先,就像 Joel 在评论中提出的那样,您使用small
和large
作为变量,但希望它们成为列表。 Second, you have used edgelist
instead of nodelist
in draw_networkx_nodes
.其次,你必须使用edgelist
代替的nodelist
中draw_networkx_nodes
。 I replaced the nx.draw
with nx.draw_networkx_edges
(and added plt.axis("off")
) to allow other users drawing smaller or larger node sizes than the default size, because smaller sizes would not work with nx.draw
.我更换了nx.draw
与nx.draw_networkx_edges
(并添加plt.axis("off")
以允许其他用户绘制更小或更大尺寸的节点比默认的大小,因为较小的尺寸不会与工作nx.draw
。
As last personal recommendation, I would replace variable names, such as d
, G
, or small
, with longer self explanatory names, like node_degrees
, graph
, node_with_low_degrees
.作为最后的个人建议,我会将变量名称(例如d
、 G
或small
)替换为更长的自解释名称,例如node_degrees
、 graph
、 node_with_low_degrees
。
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)
large = []
small = []
for node in G:
if d[node] > 4:
large.append(node)
else:
small.append(node)
print("Small", small)
print("Large", large)
nx.draw_networkx_edges(G, layout)
nx.draw_networkx_nodes(G, layout, nodelist=large, node_size=100)
nx.draw_networkx_nodes(G, layout, nodelist=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.axis("off")
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.