【发布时间】:2014-11-02 16:32:39
【问题描述】:
我有什么:具有四个节点和四个边的多重图。每个节点都有一个指定的位置(节点形成一个小队结构)
我想要什么:使用节点位置绘制多重图。
问题:位置被忽略在最终布局中。
这是我的代码:
import networkx as nx
import pydot
from my_lib import *
graph= nx.MultiGraph()
#add 4 nodes in the vertexs of a square. X and Y are the coordinates
graph.add_node(1,x=10,y=10)
graph.add_node(2,x=10,y=20)
graph.add_node(3,x=20,y=10)
graph.add_node(4,x=20,y=20)
graph.add_edge(1,2)
graph.add_edge(2,3)
graph.add_edge(3,4)
graph.add_edge(4,1)
#transform the multigraph in pydot for draw it
graphDot=nx.to_pydot(graph)
#change some attribute for the draw, like shape of nodes and the position of nodes
for node in graphDot.get_nodes():
node.set_shape('circle')
#getAttrDot is a function that returns the value of attribute passed
pos_string='\''+ get_attrDot(node,'x')+','+get_attrDot(node,'y')+'!\''
print 'coordinate: ' + pos_string #the pos_string printed is correct form: 'x,y!'
node.set('pos',pos_string)
graphDot.write_png('test_position.png')
这里是这段代码的结果。
图片'test_position.png'是:[1]:http://imgur.com/dDj3xFl
如你所见,节点位置被忽略了。
你能帮帮我吗? 谢谢!
编辑已解决: Aric 的建议解决了我的问题。谢谢!!!
【问题讨论】:
标签: python graph position draw networkx