【问题标题】:Graphviz dot layout type errorGraphviz 点布局类型错误
【发布时间】:2014-06-06 14:26:59
【问题描述】:

我想产生一棵从根(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%%%%%" 

然后我没有得到错误

  1. 如何在不编辑 n 本身的情况下更改我的代码?
  2. 为什么会这样?

【问题讨论】:

    标签: python graphviz networkx


    【解决方案1】:

    可能是 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()
    

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多