CoolFace
Apppublic

kalpkanungo/SceneGraphNet

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
visualization.py79 linesDownload Raw Back to src
1import plotly.graph_objects as go2import networkx as nx3 4 5def visualize_graph(G):6    pos = nx.spring_layout(G, seed=42)7 8    edge_x = []9    edge_y = []10    edge_text = []11 12    for u, v, data in G.edges(data=True):13        x0, y0 = pos[u]14        x1, y1 = pos[v]15 16        edge_x += [x0, x1, None]17        edge_y += [y0, y1, None]18        edge_text.append(data["label"])19 20    edge_trace = go.Scatter(21    x=edge_x,22    y=edge_y,23    line=dict(width=1),24    hoverinfo='none',25    mode='lines'26)27 28    node_x = []29    node_y = []30    node_text = []31 32    for node in G.nodes():33        x, y = pos[node]34        node_x.append(x)35        node_y.append(y)36        node_text.append(node)37 38    node_trace = go.Scatter(39        x=node_x,40        y=node_y,41        mode='markers+text',42        text=node_text,43        textposition="top center",44        hoverinfo='text',45        marker=dict(size=20)46    )47 48    fig = go.Figure(data=[edge_trace, node_trace])49 50    fig.update_layout(51        showlegend=False,52        hovermode='closest',53        margin=dict(b=20, l=5, r=5, t=40)54    )55    for u, v in G.edges():56        x0, y0 = pos[u]57        x1, y1 = pos[v]58 59        fig.add_annotation(60    x=x1,61    y=y1,62    ax=x0,63    ay=y0,64    xref='x',65    yref='y',66    axref='x',67    ayref='y',68    showarrow=True,69    70    arrowhead=4,        71    arrowsize=2.5,      72    arrowwidth=2.5,     73    arrowcolor="black", 74 75    opacity=0.976)77 78    return fig79