How to add node and edge attributes
See original GitHub issueHi
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:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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 setN.graph["graph"]
,N.graph["node"]
, andN.graph["edge"]
before callingnx.drawing.nx_pydot.to_pydot()
, NetworkX should include them as defaults in the pydot graph. For example:Output:
This is what you mean, right?
For posterity’s sake, showing some different ways:
Output:
Alternatively, you can access
G.obj_dict['attributes']
directly, but I assume that is discouraged.