[Python] Is there a way to draw the graph "features" with networkx in Python?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a way to draw the graph "features" with networkx ?

    (-1, 0 and 1 in my example)

    With my Python code, currently it does not draw it. See code below:

    [​IMG]

    import torch
    from torch_geometric.data import Data

    edge_index = torch.tensor(
    [
    [0, 1, 1, 2],
    [1, 0, 2, 1]],
    dtype=torch.long)

    #features: one feature for each node
    #node features <------
    x = torch.tensor([[-1], [0], [1]], dtype=torch.float)

    data = Data(x=x, edge_index=edge_index)

    do_visualize=1

    if do_visualize:
    import networkx as nx
    import matplotlib.pyplot as plt

    edge_list = edge_index.t().tolist()

    G = nx.Graph()
    G.add_edges_from(edge_list)
    G.add_nodes_from(range(x.size(0)))

    pos = nx.spring_layout(G, seed=1) # fixed seed for reproducibility

    fig=plt.figure(figsize=(6, 6))
    nx.draw_networkx(G, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=800)
    plt.title("Visualization of the Social Network Graph")
    plt.axis('off')
    plt.show()

    Continue reading...

Compartilhe esta Página