栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

NetworkX节点标签的相对位置

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NetworkX节点标签的相对位置

为了在轴外显示标签,您需要使轴与图形相比较小,例如,通过在轴周围引入较大的边距。您还需要设置文本的剪辑状态,以使其不会被轴切断。

根据边界框的宽度定位标签将需要先将边界框从显示共形转换为数据坐标。

完整的解决方案:

import numpy as npimport networkx as nximport matplotlib.pyplot as pltn = 7G = 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] = thetapos = {}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 graphfig, ax = plt.subplots(figsize=(5,5))margin=0.33fig.subplots_adjust(margin, margin, 1.-margin, 1.-margin)ax.axis('equal')nx.draw(G,pos=pos,with_labels=True, ax=ax)description = nx.draw_networkx_labels(G,pos,labels=labels)r = fig.canvas.get_renderer()trans = plt.gca().transData.inverted()for node, t in description.items():    bb = t.get_window_extent(renderer=r)    bbdata = bb.transformed(trans)    radius = 1.2+bbdata.width/2.    position = (radius*np.cos(angle_dict[node]),radius* np.sin(angle_dict[node]))    t.set_position(position)    t.set_rotation(angle_dict[node]*360.0/(2.0*np.pi))    t.set_clip_on(False)plt.show()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号