Python 是否可以根据节点大小更改字体大小?
根据,Python 是否可以根据节点大小更改字体大小?,python,python-3.x,networkx,Python,Python 3.x,Networkx,根据, node\u size可以是标量或数组,但font\u size必须是整数。如果节点较大,如何将字体大小更改为较大?实际上,是否可以根据节点大小更改字体大小?实际上没有传递字体大小数组的方法。和仅接受整数作为所有标签的字体大小。您必须在节点上循环,并通过matplotlib指定一些大小来添加文本。以下是一个与节点度成比例缩放的示例: from matplotlib.pyplot import figure, text G=nx.Graph() e=[(1,2),(1,5),(2,3),
node\u size
可以是标量或数组,但font\u size
必须是整数。如果节点较大,如何将字体大小更改为较大?实际上,是否可以根据节点大小更改字体大小?实际上没有传递字体大小数组的方法。和仅接受整数作为所有标签的字体大小。您必须在节点上循环,并通过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')
非常感谢@yatu
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')