[Python] Python: networkx: How to make node size auto-expand to fit the label

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using this bit of code from a deap symbolic regression example problem and the graph displays fine but I want the nodes to expand as rounded rectangles to fit the text automatically. (I don't want to just specify the node size through trial and error). How would I do that?

    # show tree
    import matplotlib.pyplot as plt
    import networkx

    nodes, edges, labels = gp.graph(bests[0])
    graph = networkx.Graph()
    graph.add_nodes_from(nodes)
    graph.add_edges_from(edges)
    pos = networkx.graphviz_layout(graph, prog="dot")

    plt.figure(figsize=(7,7))
    networkx.draw_networkx_nodes(graph, pos, node_size=900, node_color="w")
    networkx.draw_networkx_edges(graph, pos)
    networkx.draw_networkx_labels(graph, pos, labels)
    plt.axis("off")
    plt.show()

    Continue reading...

Compartilhe esta Página