have made an igraph tree with python, but my tree is plotted upside down. my top note "HoPE" starts at the bottom like this:enter image description here

我希望我的顶部 node "Hope"从屏幕的顶部开始,然后向下工作.而不是像照片那样颠倒.我试着摆弄布局,但我想不通

这是我使用的代码:

import igraph
import plotly.graph_objects as go

# Create an empty graph
g = igraph.Graph(directed=True)

# Add vertices with labels
g.add_vertex("HoPE")



# add FA node
for a in ['FA']:
    fa_node = f"HoPE_{a}"
    g.add_vertex(fa_node)
    g.add_edge('HoPE', fa_node)

# add exp node
for b in ['exp']:
    exp_node = f"HoPE_{a}_{b}"
    g.add_vertex(exp_node)
    g.add_edge(fa_node, exp_node)

# add TX node
for c in ['TX']:
    tx_node = f"HoPE_{a}_{b}_{c}"
    g.add_vertex(tx_node)
    g.add_edge(exp_node, tx_node)

# Add C nodes
for condition in ["C1", "C2", "C3", "C4"]:
    c_nodes = f"HoPE_FA_exp_TX_{condition}"
    g.add_vertex(c_nodes)
    g.add_edge(tx_node, c_nodes)

    # Add TR nodes
    for i in range(1, 4):  # TR1, TR2, TR3
        tr_node = f"HoPE_{a}_{b}_{c}_{condition}_TR{i}"
        g.add_vertex(tr_node)
        g.add_edge(c_nodes, tr_node)

        # Add 5 AR nodes for each TR node
        for j in range(1, 6):  # AR1, AR2, AR3, AR4, AR5
            ar_node = f"HoPE_{a}_{b}_{c}_{condition}_TR{i}_AR{j}"
            g.add_vertex(ar_node)
            g.add_edge(tr_node, ar_node)

# Convert igraph layout to Plotly format
layt = g.layout("rt")

# Get edge coordinates
edge_x, edge_y = [], []
for edge in g.get_edgelist():
    x0, y0 = layt[edge[0]]
    x1, y1 = layt[edge[1]]
    edge_x.extend([x0, x1, None])
    edge_y.extend([y0, y1, None])

# Get vertex coordinates and labels
node_x, node_y = zip(*[layt[vertex] for vertex in g.vs.indices])
# node_labels = [str(label) for label in g.vs.indices]
node_labels = g.vs['name']

# Create Plotly trace for edges
edge_trace = go.Scatter(
    x=edge_x,
    y=edge_y,
    line=dict(width=0.5, color="#888"),
    hoverinfo="none",
    mode="lines",
)

# Create Plotly trace for nodes
node_trace = go.Scatter(
    x=node_x,
    y=node_y,
    mode="markers",
    hoverinfo="text",
    marker=dict(showscale=True, colorscale="YlGnBu", size=10),
    text=node_labels,  # Display node labels on hover
)

# Create a figure and add traces
fig = go.Figure(data=[edge_trace, node_trace])

# Customize layout
fig.update_layout(
    showlegend=True,
    hovermode="closest",
    margin=dict(b=0, l=0, r=0, t=0),
    xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
    yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
)

# Show the interactive plot
fig.show()
print(node_labels)

推荐答案

update_layout中将autorange="reversed"yaxis相加

fig.update_layout(
    showlegend=True,
    hovermode="closest",
    margin=dict(b=0, l=0, r=0, t=0),
    xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
    yaxis=dict(showgrid=False, zeroline=False, showticklabels=False, autorange="reversed"),
)

enter image description here

Python相关问答推荐

使文本输入中的文本与标签中的文本相同

将轨迹优化问题描述为NLP.如何用Gekko解决这个问题?当前面临异常:@错误:最大方程长度错误

用gekko解决的ADE方程系统突然不再工作,错误消息异常:@错误:模型文件未找到.& &

将HTML输出转换为表格中的问题

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

从dict的列中分钟

如何在python xsModel库中定义一个可选[December]字段,以产生受约束的SON模式

如何在Python脚本中附加一个Google tab(已经打开)

driver. find_element无法通过class_name找到元素'""

如何并行化/加速并行numba代码?

Pandas GroupBy可以分成两个盒子吗?

Python中的变量每次增加超过1

如何在Python中使用Iscolc迭代器实现观察者模式?

一个telegram 机器人应该发送一个测验如何做?""

你能把函数的返回类型用作其他地方的类型吗?'

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

Python:从目录内的文件导入目录

我怎么才能用拉夫分拣呢?

查找查找表中存在的列值组合

时长超过24小时如何从Excel导入时长数据