简体   繁体   English

绘图树图不创建根节点

[英]Plotly Tree Graph Not Creating The Root Node

I'm trying to to show plotly tree graph with strings as nodes.我试图用字符串作为节点来显示树状图。 I'm adding the root node on top as to follow the sequence inside the list "l" with "david" as to be the root node.我在顶部添加根节点,以遵循列表“l”中的序列,将“david”作为根节点。 however the graph shows root node to be something else.然而,图表显示根节点是别的东西。 Any help will be highly appreciated.任何帮助将不胜感激。

Code generating the graph:生成图形的代码:

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

Tree Output:树输出:

在此处输入图片说明

Thanks谢谢

I'm not an igraph specialist, so my suggestion may have some flaws, but it will enable you to set 'david' as the root node by simply changing the line:我不是igraph专家,所以我的建议可能有一些缺陷,但它可以让您通过简单地更改行来将'david'设置为根节点:

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

to:到:

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

This seems to be because G is constructed using:这似乎是因为G是使用以下方法构造的:

G=Graph()

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

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

And vertices is:顶点是:

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

And 0 in lay = G.layout_reingold_tilford(mode="in", root=0) sets 'claire' as the root node.0 lay = G.layout_reingold_tilford(mode="in", root=0)'claire'为根节点。 So it turns out that you can set thee root to any element in ['claire', 'david', 'jenni', 'john', 'mavri'] by specifying the corresponding index.所以事实证明,您可以通过指定相应的索引将根设置为['claire', 'david', 'jenni', 'john', 'mavri']的任何元素。 So lay = G.layout_reingold_tilford(mode="in", root=1) produces the plot below.所以lay = G.layout_reingold_tilford(mode="in", root=1)产生下面的图。 And you can verify my findings by changing 1 to something else.您可以通过将1更改为其他内容来验证我的发现。

在此处输入图片说明

Complete code:完整代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM