我想从root(n)生成一棵树到它的孩子2,3:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
n = "%%%%% \n% % % \n%%%%%"
G.add_edge(n,2)
G.add_edge(n,3)
nx.write_dot(G,'test.dot')
pos=nx.graphviz_layout(G,prog='dot')
nx.draw(G,pos)
plt.show()
这会出错:
TypeError: coercing to Unicode: need string or buffer, NoneType found
如果我在'x'
之前n
使用任何字母'%'
,例如
n = "x%%%%% \n% % % \n%%%%%"
然后我没有收到错误
n
的情况下更改我的代码?答案 0 :(得分:0)
可能是graphviz不允许节点ID以%开头?
这是一种解决方法:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
n = "%%%%% \n% % % \n%%%%%"
G.add_node(1,label=n)
G.add_node(2,label=2)
G.add_node(3,label=3)
G.add_edge(1,2)
G.add_edge(1,3)
print G.edges()
nx.write_dot(G,'test.dot')
pos=nx.graphviz_layout(G,prog='dot')
nx.draw(G,pos)
labels = nx.get_node_attributes(G,"label")
nx.draw_networkx_labels(G,pos,labels=labels)
plt.show()