2012-10-15 4 views
4

나는 파이썬에서 그래프의 클릭 가능한 이미지를 생성하려고합니다. 처음에 직접 graphviz를 호출 한 다음 네트워크 x http://networkx.lanl.gov을 발견했습니다.networkx로 클릭 가능한 그래프 만들기

나는 사용자가 그래프를 클릭 한 (x, y) 좌표에 어떤 노드가 표시되는지에 대한 정보를 내 프로그램에서 얻고 싶습니다. 마우스를 클릭 할 때 (x, y) 좌표를 사용하여 그래프를 열고 표시하는 pyplot 창과 작업 할 수 있다고 생각합니다. 그러나 노드가 해당 좌표에서 시각화 된 것을 알기 위해 어떤 종류의 imagemap이 필요합니다!

할 수 있는지 여부/말할 수 있습니까?

답변

4

나는 http://groups.google.com/group/networkx-discuss (http://groups.google.com/group/networkx-discuss/browse_thread/thread/aac227e1fb2a4719)에서 좋은 사람에에게 감사를 해결 :

다음 (부분) 코드는 Tkinter를 작동하는이 포함 (그런데 비 차단,)를하기 matplotlib 창의 만들 수 있습니다 networkx 그래프를 생성하고 주어진 노드를 클릭하면 visitNode() 프로 시저를 실행합니다.

import networkx as nx 
import matplotlib.pyplot as plt 
import pylab 

class AnnoteFinder: # thanks to http://www.scipy.org/Cookbook/Matplotlib/Interactive_Plotting 
    """ 
    callback for matplotlib to visit a node (display an annotation) when points are clicked on. The 
    point which is closest to the click and within xtol and ytol is identified. 
    """ 
    def __init__(self, xdata, ydata, annotes, axis=None, xtol=None, ytol=None): 
     self.data = zip(xdata, ydata, annotes) 
     if xtol is None: xtol = ((max(xdata) - min(xdata))/float(len(xdata)))/2 
     if ytol is None: ytol = ((max(ydata) - min(ydata))/float(len(ydata)))/2 
     self.xtol = xtol 
     self.ytol = ytol 
     if axis is None: axis = pylab.gca() 
     self.axis= axis 
     self.drawnAnnotations = {} 
     self.links = [] 

    def __call__(self, event): 
     if event.inaxes: 
      clickX = event.xdata 
      clickY = event.ydata 
      if self.axis is None or self.axis==event.inaxes: 
       annotes = [] 
       for x,y,a in self.data: 
        if clickX-self.xtol < x < clickX+self.xtol and clickY-self.ytol < y < clickY+self.ytol : 
         dx,dy=x-clickX,y-clickY 
         annotes.append((dx*dx+dy*dy,x,y, a)) 
       if annotes: 
        annotes.sort() # to select the nearest node 
        distance, x, y, annote = annotes[0] 
        self.visitNode(annote) 

    def visitNode(self, annote): # Visit the selected node 
     # do something with the annote value 
     print "visitNode", annote 

fig = plt.figure() ax = fig.add_subplot(111) ax.set_title('select nodes to navigate there') 

G=nx.MultiDiGraph() # directed graph G = nx.wheel_graph(5) 

pos=nx.spring_layout(G) # the layout gives us the nodes position x,y,annotes=[],[],[] for key in pos: 
    d=pos[key] 
    annotes.append(key) 
    x.append(d[0]) 
    y.append(d[1]) nx.draw(G,pos,font_size=8) 

af = AnnoteFinder(x,y, annotes) fig.canvas.mpl_connect('button_press_event', af) 

plt.show() 
+1

불완전한/작동하지 않는 코드를 정의되지 않은 이름과 깨진 식별자로 붙여 넣을 시점은 무엇입니까? 너 자신 외에 사람들을 도울 것인가? – minerals

+0

@ minerals 언급 한 문제를 해결하기 위해 수정 사항을 제출했습니다. –