本文介绍了在networkx中编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
导入networkx为nx
G = nx.Graph()
s =СукупністьЇЄ
G.add_node(s.decode('utf-8'))
nx.draw_graphviz(G )
我正在获得
追溯(最近的最后一次呼叫):
文件< input>,第1行,< module>
文件/Library/Python/2.7/site-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pylab.py,第982行,draw_graphviz
pos = nx.drawing .graphviz_layout(G,prog)
文件/Library/Python/2.7/site-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py,第257行,graphviz_layout
return pydot_layout(G = G,prog = prog,root = root,** kwds)
文件/Library/Python/2.7/site-packages/networkx-1.10-py2.7.egg/networkx/ drawing / nx_pydot.py,第296行,pydot_layout
node = node [0]
IndexError:列表索引超出范围
我尝试将 nx_pydot 文件中的#296行更改为
node = node [:2]
之后的行(#297)
pos = node.get_pos()[2:-2]
pre>
鉴于西里尔文符号需要两个字节,但没有更改。
我使用的是Python 2.7 Mac上的.5和networkx 1.10。
解决方案我收到了网络开发人员Aric的帮助。
据他说,问题是我是使用pydot接口来绘制图形似乎不符合unicode
# - * - 编码:utf-8 - * -
import networkx as nx
import matplotlib.pyplot as pl
labels = {}
graph = nx.Graph()
words = [сукупність,вдача,волосся,колектив]
用于单词:
w = word.decode('utf-8' )
graph.add_node(w)
labels [w] = w
graph.add_edge(сукупність.decode('utf-8'),вдача.decode('utf -8'))
graph.add_edge(сукупність.decode('utf-8'),волосся.decode('utf-8'))
graph.add_edge(сукупність .decode('utf-8'),колектив.decode('utf-8')
pos = nx.spring_layout(graph)
#pos = nx.drawing.pydot_layout(graph) #不适用于uni代码
#pos = nx.drawing.pygraphviz_layout(graph)#这也可以,pygraphviz
nx.draw_networkx(graph,pos = pos,labels = labels)
pl.show()
I can't draw a graph with networkx because of Cyrillic characters.
import networkx as nx G = nx.Graph() s = "СукупністьЇЄ" G.add_node(s.decode('utf-8')) nx.draw_graphviz(G)
And I'm getting
Traceback (most recent call last): File "<input>", line 1, in <module> File "/Library/Python/2.7/site-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pylab.py", line 982, in draw_graphviz pos = nx.drawing.graphviz_layout(G, prog) File "/Library/Python/2.7/site-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 257, in graphviz_layout return pydot_layout(G=G,prog=prog,root=root,**kwds) File "/Library/Python/2.7/site-packages/networkx-1.10-py2.7.egg/networkx/drawing/nx_pydot.py", line 296, in pydot_layout node = node[0] IndexError: list index out of range
I tried changing the line #296 in nx_pydot file to
node = node[:2]
and the line after that (#297) to
pos=node.get_pos()[2:-2]
given that a Cyrillic symbol takes two bytes, but nothing changed.
I'm using Python 2.7.5 on Mac and networkx 1.10.
解决方案I received help from a networkx developer, Aric.
According to him, the problem was that I was using the pydot interface to graphviz which doesn't seem to work with unicode
# -*- coding: utf-8 -*- import networkx as nx import matplotlib.pyplot as pl labels={} graph = nx.Graph() words= ["сукупність","вдача","волосся","колектив"] for word in words: w = word.decode('utf-8') graph.add_node(w) labels[w]=w graph.add_edge("сукупність".decode('utf-8'),"вдача".decode('utf-8')) graph.add_edge("сукупність".decode('utf-8'),"волосся".decode('utf-8')) graph.add_edge("сукупність".decode('utf-8'),"колектив".decode('utf-8')) pos=nx.spring_layout(graph) # pos=nx.drawing.pydot_layout(graph) # doesn't work with unicode # pos=nx.drawing.pygraphviz_layout(graph) # this also works, pygraphviz nx.draw_networkx(graph, pos=pos, labels=labels) pl.show()
这篇关于在networkx中编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!