在draw_hierarchy中径向对齐节点标签

时间:2018-03-25 16:52:02

标签: python matplotlib networkx graph-tool

如何使用“draw_hierarchy”将节点标签对齐到径向朝向中心?包含我的网络数据集的文本文件在每行中都有两个连接节点的标签(data)。

这是我的代码:

for i in range(len(events["e"])):
    g = Graph(directed=False)
    vprop = g.new_vertex_property("string") 
    eprop = g.new_edge_property("int") 
    added_nodes=[]
    n=0
    for ii, row in data.iterrows():
        #if the node does not already exist in the node list labels
        if data['i'][ii] not in added_nodes:
           added_nodes.append(data['i'][ii])
           exec("vertex_%d=g.add_vertex()" %n)
           exec("vprop[vertex_%d] =%d"%(n  , data['i'][ii]))
           vertix_out=n
           n+=1
        else:
           indx= added_nodes.index(data['i'][ii])
           vertix_out=indx

        if data['j'][ii] not in added_nodes: 
           added_nodes.append(data['j'][ii]) 
           exec("vertex_%d=g.add_vertex()" %n)
           exec("vprop[vertex_%d] =%d"%(n  , data['j'][ii]))
           vertix_in=n
           n+=1
        else:
           indx= added_nodes.index(data['j'][ii]) 
           vertix_in=indx
        exec("e=g.add_edge(vertex_%d,vertex_%d)"%(vertix_out,vertix_in))
        eprop[e] =data['t_sp'][ii]
    g.vertex_properties["name"]=vprop 
    g.edge_properties["Timestamp"]=eprop
    state = minimize_nested_blockmodel_dl(g, deg_corr=True,  mcmc_args={'parallel':True},mcmc_equilibrate_args={'verbose':False, 'epsilon':1e-4}, verbose=True)
    L = len(state.levels)
    # ## block membership of each node on each level
    eventName = events["e"][i]
    for l in range(L):
         with open(os.path.join('/home/','nodes_blocks_level_%s_%s'%(l,eventName)),'w') as f:
              state_proj_l = state.project_level(l)
              blocks_proj_l = state_proj_l.get_blocks()
              for i_b,b in enumerate(blocks_proj_l.a):
                  f.write('%s \t %s \n'%(i_b,b))     
    print state.entropy()    

    t = get_hierarchy_tree(state)[0]
    tpos = pos = radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
    cts = get_hierarchy_control_points(g, t, tpos,beta=.87)
    pos = g.own_property(tpos)
    #text rotation
    vtext_rotation = g.new_vertex_property('double')
    for v in g.vertices():
        if pos[v][0] >= 0:
           try:
               vtext_rotation[v] = math.atan(pos[v][1]/pos[v][0])
           except ZeroDivisionError:
               vtext_rotation[v] = 0
        else:
            vtext_rotation[v] = math.pi + math.atan(pos[v][1]/pos[v][0])
    g.vertex_properties['text_rotation'] = vtext_rotation
    """
    graph_draw(g, pos=pos, 
               edge_control_points=cts,
               vertex_size=20,
               vertex_text=g.vertex_properties["name"], 
               vertex_text_position=1, 
               vertex_text_rotation=g.vertex_properties['text_rotation'], 
               vertex_font_family='mono',
               vertex_font_size=5,
               vertex_anchor=0, 
               output_size=[1024*2,1024*2],
               output="ScioPattern_%s.png"%(eventName))
    """           
    draw_hierarchy(state, pos=pos, 
               deg_order=True,    
               edge_control_points=cts,
               vertex_size=20,
               vertex_text=g.vertex_properties["name"], 
               vertex_text_position=1, 
               vertex_text_rotation=g.vertex_properties['text_rotation'], 
               vertex_font_family='mono',
               vertex_font_size=15,
               vertex_anchor=0, 
               output_size=[1024*2,1024*2],
               output="hierarchy_%s.png"%(eventName))

当我使用“graph_draw”时,标签会径向对齐,但当我使用“draw_hierarchy”时,它们会随机定向。为什么会这样? enter image description here

我也想知道如何构建分层块模型?我应该使用LayeredBlockState课吗?有人可能提供一个如何运作的例子吗?

1 个答案:

答案 0 :(得分:2)

只需将vertex_text_position="centered"选项传递给draw_hierarchy()

在HOWTO中有一个使用分层SBM的示例:https://graph-tool.skewed.de/static/doc/demos/inference/inference.html#layered-networks