Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 固定位置多重图的绘制节点_Python_Graph_Position_Draw_Networkx - Fatal编程技术网

Python 固定位置多重图的绘制节点

Python 固定位置多重图的绘制节点,python,graph,position,draw,networkx,Python,Graph,Position,Draw,Networkx,我拥有的:一个具有四个节点和四条边的多重图。每个节点都有一个指定的位置(节点形成一个方形结构) 我想要什么:使用节点位置绘制多重图。 问题:在最终布局中忽略位置。 这是我的代码: 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_n

我拥有的:一个具有四个节点和四条边的多重图。每个节点都有一个指定的位置(节点形成一个方形结构)
我想要什么:使用节点位置绘制多重图。
问题:在最终布局中忽略位置。

这是我的代码:

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]:

如您所见,节点位置将被忽略

你能帮我吗? 谢谢


编辑解决:阿里的建议解决了我的问题。谢谢

您可以在将图形转换为Pydot对象之前设置属性:

import networkx as nx                                                                                     
graph= nx.MultiGraph()                                                                                    

#add 4 nodes in the vertexs of a square. X and Y are the coordinates                                      
graph.add_node(1,x=100,y=100)                                                                             
graph.add_node(2,x=100,y=200)                                                                             
graph.add_node(3,x=200,y=100)                                                                             
graph.add_node(4,x=200,y=200)                                                                             
graph.add_edge(1,2)                                                                                       
graph.add_edge(2,3)                                                                                       
graph.add_edge(3,4)                                                                                       
graph.add_edge(4,1)                                                                                       
                                                                                                         # assign positions                                                                                        
for n in graph:                                                                                           
    graph.node[n]['pos'] = '"%d,%d"'%(graph.node[n]['x'], graph.node[n]['y'])                             
p = nx.to_pydot(graph)                                                                                    
print p.to_string()                                                                                       
p.write('foo.dot')                                                                                        
# run neato -n2 -Tpng foo.dot >foo.png  
输出为:

graph G {
1 [y=100, x=100, pos="100,100"];
2 [y=200, x=100, pos="100,200"];
3 [y=100, x=200, pos="200,100"];
4 [y=200, x=200, pos="200,200"];
1 -- 2  [key=0];
1 -- 4  [key=0];
2 -- 3  [key=0];
3 -- 4  [key=0];
}
neato-n2-Tpng foo.dot>foo.png
(n2保持节点位置)


谢谢你的回答。但是当使用命令行neato-n2-Tpng foo.dot>foo.png时,windows的提示命令会说:neato无法打开foo.dot。为什么?