Plotly 树图不创建根节点

Plotly Tree Graph Not Creating The Root Node

我正在尝试显示以字符串为节点的 plotly 树图。我在顶部添加根节点,以遵循列表“l”中的顺序,“david”作为根节点。但是该图显示根节点是其他东西。任何帮助将不胜感激。

生成图表的代码:

import igraph
from igraph import Graph, EdgeSeq
nr_vertices = 25

import plotly.offline as pyo
pyo.init_notebook_mode()


l=[('david','john'),
   ('david','jenni'), 
    ('john','jenni'),
   ('john','david'),
   ('john','mavri'),
   ('john','claire'),
   ]

    
vertices = set()
for line in l:
    vertices.update(line)
vertices = sorted(vertices)

print(len(l))

v_label = list(map(str, range(len(vertices))))
v2 = list(map(str, vertices))
#g = Graph.TupleList(directed=True, edges = l) 
G=Graph()

#G.add_vertices(l)
G.add_vertices(vertices)

# add edges to the graph
G.add_edges(l)


lay = G.layout_reingold_tilford(mode="in", root=0)
print(G)

position = {k: lay[k] for k in range(len(vertices))}

print(position)

Y = [lay[k][1] for k in range(len(vertices))]

M = max(Y)


es = EdgeSeq(G) # sequence of edges
E = [e.tuple for e in G.es] # list of edges
print(E)

L = len(position)

Xn = [position[k][0] for k in range(L)]

Yn = [2*M-position[k][1] for k in range(L)]
Xe = []
Ye = []

for edge in E:
    Xe+=[position[edge[0]][0],position[edge[1]][0], None]
    Ye+=[2*M-position[edge[0]][1],2*M-position[edge[1]][1], None]

labels = v2
print(Xe)

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=Xe,
                   y=Ye,
                   mode='lines',
                   line=dict(color='rgb(210,210,210)', width=1),
                   hoverinfo='none'
                   ))
fig.add_trace(go.Scatter(x=Xn,
                  y=Yn,
                  mode='markers',
                  name='bla',
                  marker=dict(symbol='circle-dot',
                                size=30,
                                color='#6175c1',    #'#DB4551',
                                line=dict(color='rgb(50,50,50)', width=1)
                                ),
                  text=labels,
                  hoverinfo='text',
                  opacity=0.8
                  ))

def make_annotations(pos, text, font_size=10, font_color='rgb(250,250,250)'):
    L=len(pos)
    if len(text)!=L:
        raise ValueError('The lists pos and text must have the same len')
    annotations = []
    for k in range(L):
        annotations.append(
            dict(
                text=labels[k], # or replace labels with a different list for the text within the circle
                x=pos[k][0], y=2*M-position[k][1],
                xref='x1', yref='y1',
                font=dict(color=font_color, size=font_size),
                showarrow=False)
        )
    return annotations

axis = dict(showline=False, # hide axis line, grid, ticklabels and  title
            zeroline=False,
            showgrid=False,
            showticklabels=False,
            )

fig.update_layout(title= 'Tree with Reingold-Tilford Layout',
              annotations=make_annotations(position, v_label),
              font_size=12,
              showlegend=False,
              xaxis=axis,
              yaxis=axis,
              margin=dict(l=40, r=40, b=85, t=100),
              hovermode='closest',
              plot_bgcolor='rgb(248,248,248)'
              )
fig.show()

树输出:

谢谢

我不是 igraph 专家,所以我的建议可能有一些缺陷,但它可以让您通过简单地更改以下行将 'david' 设置为根节点:

lay = G.layout_reingold_tilford(mode="in", root=0)

至:

lay = G.layout_reingold_tilford(mode="in", root=1)

这似乎是因为 G 是使用以下方法构造的:

G=Graph()

#G.add_vertices(l)
G.add_vertices(vertices)

# add edges to the graph
G.add_edges(l)

顶点是:

['claire', 'david', 'jenni', 'john', 'mavri']

lay = G.layout_reingold_tilford(mode="in", root=0)中的0设置'claire'为根节点。所以事实证明,您可以通过指定相应的索引将根设置为 ['claire', 'david', 'jenni', 'john', 'mavri'] 中的任何元素。所以 lay = G.layout_reingold_tilford(mode="in", root=1) 产生了下面的情节。您可以通过将 1 更改为其他内容来验证我的发现。

完整代码:

import igraph
from igraph import Graph, EdgeSeq
nr_vertices = 25

import plotly.offline as pyo
pyo.init_notebook_mode()


l=[('david','john'),
   ('david','jenni'), 
    ('john','jenni'),
   ('john','david'),
   ('john','mavri'),
   ('john','claire'),
   ]

    
vertices = set()
for line in l:
    vertices.update(line)
vertices = sorted(vertices)

print(len(l))

v_label = list(map(str, range(len(vertices))))
v2 = list(map(str, vertices))
#g = Graph.TupleList(directed=True, edges = l) 
G=Graph()

#G.add_vertices(l)
G.add_vertices(vertices)

# add edges to the graph
G.add_edges(l)


lay = G.layout_reingold_tilford(mode="in", root=1)
print(G)

position = {k: lay[k] for k in range(len(vertices))}

print(position)

Y = [lay[k][1] for k in range(len(vertices))]

M = max(Y)


es = EdgeSeq(G) # sequence of edges
E = [e.tuple for e in G.es] # list of edges
print(E)

L = len(position)

Xn = [position[k][0] for k in range(L)]

Yn = [2*M-position[k][1] for k in range(L)]
Xe = []
Ye = []

for edge in E:
    Xe+=[position[edge[0]][0],position[edge[1]][0], None]
    Ye+=[2*M-position[edge[0]][1],2*M-position[edge[1]][1], None]

labels = v2
print(Xe)

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=Xe,
                   y=Ye,
                   mode='lines',
                   line=dict(color='rgb(210,210,210)', width=1),
                   hoverinfo='none'
                   ))
fig.add_trace(go.Scatter(x=Xn,
                  y=Yn,
                  mode='markers',
                  name='bla',
                  marker=dict(symbol='circle-dot',
                                size=30,
                                color='#6175c1',    #'#DB4551',
                                line=dict(color='rgb(50,50,50)', width=1)
                                ),
                  text=labels,
                  hoverinfo='text',
                  opacity=0.8
                  ))

def make_annotations(pos, text, font_size=10, font_color='rgb(250,250,250)'):
    L=len(pos)
    if len(text)!=L:
        raise ValueError('The lists pos and text must have the same len')
    annotations = []
    for k in range(L):
        annotations.append(
            dict(
                text=labels[k], # or replace labels with a different list for the text within the circle
                x=pos[k][0], y=2*M-position[k][1],
                xref='x1', yref='y1',
                font=dict(color=font_color, size=font_size),
                showarrow=False)
        )
    return annotations

axis = dict(showline=False, # hide axis line, grid, ticklabels and  title
            zeroline=False,
            showgrid=False,
            showticklabels=False,
            )

fig.update_layout(title= 'Tree with Reingold-Tilford Layout',
              annotations=make_annotations(position, v_label),
              font_size=12,
              showlegend=False,
              xaxis=axis,
              yaxis=axis,
              margin=dict(l=40, r=40, b=85, t=100),
              hovermode='closest',
              plot_bgcolor='rgb(248,248,248)'
              )
fig.show()