question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add node and edge attributes

See original GitHub issue

Hi

Is there a way to add node and edge attributes. Not successful via networkx. Does pydot give an option?

Below is the code, with not working section commented.

#  NETWORX TO PYDOT TO GRAPHVIZ MODULE
P = nx.nx_pydot.to_pydot(G)

# before sending to neato, fix the pos values
# 1. A [pos="(21.31227, 46.18656)"]; should become A [pos="21.31227, 46.18656!"];
P1 = str(P).replace('"(', '"').replace(')"','!"')
print(P1)
M = Source(P1, engine='neato',format='png')

# NOT WORKING START #-------------------
# M.attr(overlap='compress')
# M.attr(sep='3')
# M.attr('edge', fontsize='9', color='grey', fontcolor='#7c7c7c')
# M.attr('node', shape='box', width='0.1', height='0.1', style='filled', fontname="Arial", fontsize='10', fixedsize='true', color="#666666")
# NOT WORKING END #---------------------

M.render('test1', view=True)  

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
peternoweecommented, Sep 22, 2021

Hmm, yes, I see. I do not know NetworkX that well, but looking at the source code, it does seem to try to set some defaults at the start of the export:

    name = N.name
    graph_defaults = N.graph.get("graph", {})
    if name == "":
        P = pydot.Dot("", graph_type=graph_type, strict=strict, **graph_defaults)
    else:
        P = pydot.Dot(
            f'"{name}"', graph_type=graph_type, strict=strict, **graph_defaults
        )
    try:
        P.set_node_defaults(**N.graph["node"])
    except KeyError:
        pass
    try:
        P.set_edge_defaults(**N.graph["edge"])
    except KeyError:
        pass

From: https://github.com/networkx/networkx/blob/networkx-2.6.3/networkx/drawing/nx_pydot.py#L197-L212

Where N is the NetworkX graph, so if we can set N.graph["graph"], N.graph["node"], and N.graph["edge"] before calling nx.drawing.nx_pydot.to_pydot(), NetworkX should include them as defaults in the pydot graph. For example:

import networkx as nx
import pydot
G = nx.Graph()
G.graph["node"] = {"style": "filled", "fillcolor": "blue"}
G.add_node("a")
graph = nx.drawing.nx_pydot.to_pydot(G)
print(graph)

Output:

strict graph  {
node [fillcolor=blue, style=filled];
a;
}

This is what you mean, right?

1reaction
peternoweecommented, Sep 9, 2020

For posterity’s sake, showing some different ways:

import pydot
G = pydot.Dot('G', overlap='compress')
G.set_sep('3')
G.set_edge_defaults(fontsize='9', color='grey', fontcolor='#7c7c7c')
G.set_node_defaults(shape='box', width='0.1', height='0.1', style='filled', fontname="Arial", fontsize='10', fixedsize='true', color="#666666")
N = pydot.Node('N')
G.add_node(N)
O = pydot.Node('O', width='0.5')
O.set_height('0.8')
G.add_node(O)
print(G)

Output:

digraph G {
overlap=compress;
sep=3;
edge [color=grey, fontcolor="#7c7c7c", fontsize=9];
node [color="#666666", fixedsize=true, fontname=Arial, fontsize=10, height="0.1", shape=box, style=filled, width="0.1"];
N;
O [height="0.8", width="0.5"];
}

Alternatively, you can access G.obj_dict['attributes'] directly, but I assume that is discouraged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tutorial — NetworkX 2.8.8 documentation
Adding attributes to graphs, nodes, and edges#. Attributes such as weights, labels, colors, or whatever Python object you like, can be attached to...
Read more >
Add Graph Node Names, Edge Weights, and Other Attributes
This example shows how to add attributes to the nodes and edges in graphs created using graph and digraph . You can specify...
Read more >
Attribute basics
You can add attributes when adding the nodes to the graph: ... We can use the G.edges() function to get all the edges...
Read more >
Node and Edge Attributes - Why Study Networks and ...
3 trial videos available. Create an account to watch unlimited course videos. Join for free. Node and Edge Attributes.
Read more >
Edge Attributes
edge [name0=val0] sets default edge attribute name0 to val0. Any edge appearing after this inherits the new default attributes. n1 -> n2 [name1= ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found