Search code examples
pythondata-structurestreeigraph

how do I flip my igraph tree in python. so that it isn't upside down


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

i want my top node "HoPE" to start at the top of the screen and work his way down. and not upside down like the picture. I tried playing with the layout but I couldn't figure this out

this is the code I used:

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)

Solution

  • Add autorange="reversed" to yaxis in update_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, autorange="reversed"),
    )
    

    enter image description here