【发布时间】:2017-10-09 06:00:42
【问题描述】:
我正在努力解决以下问题。根据之前所做的分类,我想绘制一个包含大约 100 个节点的圆形图,我必须在其中手动定位它们。这些节点有一个描述它们的分配标签,具有不同的文本长度,我想把这个标签放在节点旁边。下图是我想要获得的(我绘制蓝色圆圈只是为了表明标签在外围完全对齐): https://i.stack.imgur.com/Qre0Z.png
目前我只画了这个:https://i.stack.imgur.com/U7bZG.png
这是 MWE:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
n = 7
G = nx.complete_graph(n)
node_list = sorted(G.nodes())
angle = []
angle_dict = {}
for i, node in zip(xrange(n),node_list):
theta = 2.0*np.pi*i/n
angle.append((np.cos(theta),np.sin(theta)))
angle_dict[node] = theta
pos = {}
for node_i, node in enumerate(node_list):
pos[node] = angle[node_i]
labels = {0:'zero',1:'oneone',2:'twotwo',3:'threethreethree',4:'fourfourfourfour',5:'fivefivefivefivefive',6:'sixsixsixsixsixsix'}
# figsize is intentionally set small to condense the graph
f = plt.figure(figsize=(2,2))
r = f.canvas.get_renderer()
plt.axis('equal')
nx.draw(G,pos=pos,with_labels=True)
description = nx.draw_networkx_labels(G,pos,labels=labels)
for node, t in description.items():
t.set_rotation(angle_dict[node]*360.0/(2.0*np.pi))
plt.show()
我想我必须添加和玩
x, y = t.get_position()
bb = t.get_window_extent(renderer=r)
radius = 1.0+2.0*bb.width/r.width
t.set_position((radius*x,radius*y))
在我设置标签旋转的循环中。但是,我不知道如何正确设置它以及如何避免裁剪画布。
【问题讨论】:
标签: python matplotlib graph networkx