Python 不创建根节点的Plotly树形图
我正在尝试用字符串作为节点来显示plotly树形图。我将根节点添加到顶部,以遵循列表“l”中的顺序,并将“david”作为根节点。但是,该图显示根节点是其他节点。我们将非常感谢您的帮助 生成图形的代码:Python 不创建根节点的Plotly树形图,python,matplotlib,graph,plotly,networkx,Python,Matplotlib,Graph,Plotly,Networkx,我正在尝试用字符串作为节点来显示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'), ('
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)
致:
这似乎是因为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']
和0
inlay=G.layout\u reingold\u tilford(mode=“in”,root=0)
将'claire'
设置为根节点。因此,通过指定相应的索引,您可以将root设置为['claire','david','jenni','john','mavri']
中的任何元素。因此lay=G.layout\u reingold\u tilford(mode=“in”,root=1)
生成下面的图。您可以通过将1
更改为其他内容来验证我的发现
完整代码:
此图不是树,没有根节点。看,大卫、约翰和杰尼之间有一个循环,谢谢。该绘图是通过从以下内容获取帮助而生成的。。
['claire', 'david', 'jenni', 'john', 'mavri']
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()