确实没有办法传递字体大小数组。两个都nx.draw https://networkx.github.io/documentation/stable/reference/generated/networkx.drawing.nx_pylab.draw.html#networkx.drawing.nx_pylab.draw and draw_networkx_labels https://networkx.github.io/documentation/stable/reference/generated/networkx.drawing.nx_pylab.draw_networkx_labels.html#networkx.drawing.nx_pylab.draw_networkx_labels仅接受整数作为所有标签的字体大小。您必须循环遍历节点并通过 matplotlib 添加指定大小的文本。这是一个示例,与节点度数成比例缩放:
from matplotlib.pyplot import figure, text
G=nx.Graph()
e=[(1,2),(1,5),(2,3),(3,6),(5,6),(4,2),(4,3),(3,5),(1,3)]
G.add_edges_from(e)
pos = nx.spring_layout(G)
figure(figsize=(10,6))
d = dict(G.degree)
nx.draw(G, pos=pos,node_color='orange',
with_labels=False,
node_size=[d[k]*300 for k in d])
for node, (x, y) in pos.items():
text(x, y, node, fontsize=d[node]*5, ha='center', va='center')