Python中的Networkx - 将节点属性绘制为节点外的标签

时间:2013-01-27 12:38:20

标签: python networkx

我有一个具有特定属性的节点图,我想在Python中通过networkx绘制图形,其中有几个属性作为节点外节点的标签。

有人可以帮助我如何编写代码来实现这一目标吗?

我的代码中有一个生成“interface_?”的循环来自防火墙列表(fwList)的每个输入的属性

for y in fwList:
    g.add_node(n, type='Firewall')
    print 'Firewall ' + str(n) + ' :' 
    for x in fwList[n]:
        g.node[n]['interface_'+str(i)] = x
        print 'Interface '+str(i)+' = '+g.node[n]['interface_'+str(i)]
        i+=1
    i=1
    n+=1

然后,我稍后绘制节点和边缘,如:

pos=nx.spring_layout(g)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_nodes(g,pos,nodelist=[1,2,3],node_shape='d',node_color='red')

并稍后将其扩展到一些具有其他形状和颜色的新节点。

为了标记我在代码下面尝试的单个属性,但它没有用

labels=dict((n,d['interface_1']) for n,d in g.nodes(data=True))

为了将文本放出节点我不知道......

4 个答案:

答案 0 :(得分:9)

您可以访问'pos'字典中的节点位置。因此,您可以使用matplotlib将文本放在任何您喜欢的位置。 e.g。

In [1]: import networkx as nx

In [2]: G=nx.path_graph(3)

In [3]: pos=nx.spring_layout(G)

In [4]: nx.draw(G,pos)

In [5]: x,y=pos[1]

In [6]: import matplotlib.pyplot as plt

In [7]: plt.text(x,y+0.1,s='some text', bbox=dict(facecolor='red', alpha=0.5),horizontalalignment='center')
Out[7]: <matplotlib.text.Text at 0x4f1e490>

enter image description here

答案 1 :(得分:4)

除了Aric的答案,O(lg n)字典在值中包含pos个坐标。所以你可以操纵它,例如:

x, y

然后使用新位置绘制标签:

pos_higher = {}
y_off = 1  # offset on the y axis

for k, v in pos.items():
    pos_higher[k] = (v[0], v[1]+y_off)

其中nx.draw_networkx_labels(G, pos_higher, labels) 是您的图表对象,G是字符串列表。

答案 2 :(得分:1)

NetworkX 关于 draw_networkx_labels 的文档还表明,您可以使用 horizontalalignmentverticalalignment 参数来获得简单、开箱即用的解决方案,而无需手动轻推标签。< /p>

来自docs

<块引用>
  • horizo​​ntalalignment ({‘center’, ‘right’, ‘left’}) – 水平 对齐(默认 = 'center')

  • 垂直对齐({‘center’, ‘top’, ‘bottom’, ‘baseline’, ‘center_baseline’}) – 垂直对齐(默认 = ‘center’)

更方便的是,您可以将其与更高级别的 NetworkX 函数一起使用,例如 nx.draw()nx.draw_spectral 之一(或其变体),因为这些更高级别的函数参数接受关键字参数反过来传递给它的低级函数。

所以下面的代码是一个最低限度可行的代码,它完成了你的要求,即将标签推到节点外:

G = nx.DiGraph(name='Email Social Network')
nx.draw(G, arrowsize=3, verticalalignment='bottom')
# or:
nx.draw(G, arrowsize=3, verticalalignment='top')
plt.show()

verticalalignment 的结果如下图所示: enter image description here

答案 3 :(得分:0)

我喜欢创建一个 nudge 函数,将布局移动一个偏移量。

import networkx as nx
import matplotlib.pyplot as plt

def nudge(pos, x_shift, y_shift):
    return {n:(x + x_shift, y + y_shift) for n,(x,y) in pos.items()}

G = nx.Graph()
G.add_edge('a','b')
G.add_edge('b','c')
G.add_edge('a','c')

pos = nx.spring_layout(G)
pos_nodes = nudge(pos, 0, 0.1)                              # shift the layout

fig, ax = plt.subplots(1,2,figsize=(12,6))
nx.draw_networkx(G, pos=pos, ax=ax[0])                      # default labeling
nx.draw_networkx(G, pos=pos, with_labels=False, ax=ax[1])   # default nodes and edges
nx.draw_networkx_labels(G, pos=pos_nodes, ax=ax[1])         # nudged labels
ax[1].set_ylim(tuple(i*1.1 for i in ax[1].get_ylim()))      # expand plot to fit labels
plt.show()

Two graph representations, side by side, of three nodes in a triangle. The one on the left has labels placed directly on the nodes per the networkx defaults, and the one on the right has labels shifted slightly up

相关问题