Networkx decoding to str TypeError

I am trying to create a graph representation from some text, and keep getting a TypeError. Here is an example code:

document="Let us assume, as a running example, that my data is composed of word embeddings of the\
 English language. I want to gain insights about the word distribution in the embedding space,\
 specifically, if there are any clusters of very similar words, if there are words that are\
 completely different from the rest, if there are words very similar to every other word, and so \
on. And I want to gain these insights visually, so that it is easier to understand and share with my collegues."

document = preprocess_document(document)
nodes = get_entities(document)
edges= get_relations(document)

G= nx.Graph()

pos = nx.nx_agraph.graphviz_layout(G)

nx.draw(G, pos=pos, with_labels=True)
edge_labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)

I get the error:

----> 1 plot_graph(G)

<ipython-input-174-287c83e8e288> in plot_graph(G, title)
      5     # define position of nodes in figure
----> 6     pos = nx.nx_agraph.graphviz_layout(G)
      8     # draw nodes and edges

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/ in graphviz_layout(G, prog, root, args)
    235     This is a wrapper for pygraphviz_layout.
    236     """
--> 237     return pygraphviz_layout(G, prog=prog, root=root, args=args)

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/ in pygraphviz_layout(G, prog, root, args)
    282     if root is not None:
    283         args += f"-Groot={root}"
--> 284     A = to_agraph(G)
    285     A.layout(prog=prog, args=args)
    286     node_pos = {}

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/networkx/drawing/ in to_agraph(N)
    146     # add nodes
    147     for n, nodedata in N.nodes(data=True):
--> 148         A.add_node(n)
    149         # Add node data
    150         a = A.get_node(n)

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/ in add_node(self, n, **attr)
    311         except KeyError:
    312             nh = gv.agnode(self.handle, n, _Action.create)
--> 313         node = Node(self, nh=nh)
    314         node.attr.update(**attr)

~/anaconda3/envs/vineeth/lib/python3.6/site-packages/pygraphviz/ in __new__(self, graph, name, nh)
   1562     def __new__(self, graph, name=None, nh=None):
   1563         if nh is not None:
-> 1564             n = super(Node, self).__new__(self, gv.agnameof(nh), graph.encoding)
   1565         else:
   1566             n = super(Node, self).__new__(self, name)

TypeError: decoding to str: need a bytes-like object, NoneType found

The error seems to be in the nx.nx_agraph.graphviz_layout(G) part of the code. I have tried changing the document, but the same error keeps popping up. I have manually verified that none of the nodes or edges have a NaN or are empty.

Can someone please help resolve this?


  • To make it easier for future readers I'll post this partial answer here (it doesn't really solve why your Graphviz layout doesn't work.) But your code works by change the layout like for example:

    pos = nx.spring_layout(G)