Skip to main content
`Ctrl`+`K`

Note

Go to the end to download the full example code.

# Attributes

Example illustrating how attributes of nodes, edges, and graphs are handled during conversion to/from `AGraph`.

    edges
    [('1', '2', {'color': 'red'}), ('2', '3', {'color': 'red'})]
    default graph attributes
    {'graph': {}, 'node': {}, 'edge': {'color': ''}}
    node node attributes
    [('1', {}), ('2', {}), ('3', {}), ('4', {})]

    import networkx as nx

    G = nx.Graph()
    G.add_edge(1, 2, color="red")
    G.add_edge(2, 3, color="red")
    G.add_node(3)
    G.add_node(4)

    A = nx.nx_agraph.to_agraph(G)  # convert to a graphviz graph
    A.draw("attributes.png", prog="neato")  # Draw with pygraphviz

    # convert back to networkx Graph with attributes on edges and
    # default attributes as dictionary data
    X = nx.nx_agraph.from_agraph(A)
    print("edges")
    print(list(X.edges(data=True)))
    print("default graph attributes")
    print(X.graph)
    print("node node attributes")
    print(X.nodes.data(True))

Total running time of the script: (0 minutes 0.028 seconds)

Gallery generated by Sphinx-Gallery