NetworkX节点标记相对位置 [英] NetworkX node labels relative position

查看:274
本文介绍了NetworkX节点标记相对位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决以下问题。根据以前的分类,我想绘制一个约100个节点的圆形图,我必须手动定位它们。这些节点有一个分配的标签,用不同的文本长度来描述它们,我想把这个标签放在节点的旁边。下面的图是我想要获得的(我画蓝圈只是为了表明标签完全对齐在外围):


I am struggling with the following problem. I want to plot a circular graph of about 100 nodes where I have to manually position them, according to a classification done previously. These nodes have an assigned label which describes them, with different text lengths, and I want to put this label adjacent to the node. The following graph is what I would want to obtain (I draw the blue circle only to show that the labels are perfectly aligned at the periphery): https://i.stack.imgur.com/Qre0Z.png

So far I have only achieved to draw this:https://i.stack.imgur.com/U7bZG.png

Here is the 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()

I guess I have to add and play with

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

in the loop where I set the rotation of the labels. However, I don't figure out how to set it properly as well as how to avoid to crop the canvas.

解决方案

In order to show the labels outside the axes, you would need to make the axes small compared to the figure, e.g. by introducing a large margin around the axes. You would also need to set the clip state of the text, such that it's not cut off by the axes.

Positioning the labels according to the width of the bounding box would require to transform the bounding box from display coodinates to data coordinates first.

Complete solution:

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
fig, ax = plt.subplots(figsize=(5,5))
margin=0.33
fig.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()

这篇关于NetworkX节点标记相对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆