Should work something like this:
In [1]: import networkx as nx
In [2]: G = nx.Graph()
In [3]: G.add_edge(1,2,label='one-two')
In [4]: import sys
In [5]: nx.write_dot(G,sys.stdout)
strict graph {
1 -- 2 [label="one-two"];
}
سؤال
I'm trying to add edge labels to my edges in networkx. I will use nx.write_dot()
to convert it into a dot file. How can I do this? When I do this: G.add_edge("ATC", "TCG", label = "ATCG")
, nx.write_dot()
throws an error. I like networkx, but if it can't label edges then I think I'll have to use another library.
Specifically, I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.3/site-packages/networkx/drawing/nx_agraph.py", line 180, in write_dot
A.clear()
File "/usr/local/lib/python3.3/site-packages/pygraphviz/agraph.py", line 907, in clear
self.edge_attr.clear()
File "/usr/local/Cellar/python3/3.3.3/Frameworks/Python.framework/Versions/3.3/lib/python3.3/collections/abc.py", line 553, in clear
self.popitem()
File "/usr/local/Cellar/python3/3.3.3/Frameworks/Python.framework/Versions/3.3/lib/python3.3/collections/abc.py", line 542, in popitem
key = next(iter(self))
File "/usr/local/lib/python3.3/site-packages/pygraphviz/agraph.py", line 1692, in __iter__
for (k,v) in self.iteritems():
File "/usr/local/lib/python3.3/site-packages/pygraphviz/agraph.py", line 1700, in iteritems
yield (gv.agattrname(ah).decode(self.encoding),
AttributeError: 'str' object has no attribute 'decode'
المحلول
Should work something like this:
In [1]: import networkx as nx
In [2]: G = nx.Graph()
In [3]: G.add_edge(1,2,label='one-two')
In [4]: import sys
In [5]: nx.write_dot(G,sys.stdout)
strict graph {
1 -- 2 [label="one-two"];
}