是否可以根据节点大小更改字体大小?

Is it possible to change font sizes according to node sizes?

根据NetworkX,

draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds), 

node_size 可以是标量或数组,但 font_size 必须是整数。如果节点很大,如何将字体大小更改为更大?事实上,是否可以根据节点大小更改字体大小?

实际上并没有一种传递字体大小数组的方法。 nx.draw and 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')