Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

According to NetworkX,

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

node_size can be scalar or array but font_size needs to be integer. How can I change the font size to be bigger if the nodes are big? In fact, is it possible to change font sizes according to node sizes?

like image 636
Jan Avatar asked Dec 31 '22 00:12

Jan


1 Answers

There isn't really a way of passing an array of font sizes. Both nx.draw and draw_networkx_labels only accept integers as font sizes for all labels. You'll have to loop over the nodes and add the text via matplotlib specifying some size. Here's an example, scaling proportionally to the node degree:

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')

enter image description here

like image 54
yatu Avatar answered Feb 09 '23 01:02

yatu