如何通过networkx制作可点击的图表? [英] How to make a click-able graph by networkx?

查看:42
本文介绍了如何通过networkx制作可点击的图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 中生成图形的可点击图像.一开始直接调用graphviz,后来发现networkx http://networkx.lanl.gov .

I am trying to produce a clickable image of a graph in python. I directly called graphviz at first, then discovered networkx http://networkx.lanl.gov .

我希望我的程序能够获取有关在用户单击图形的 (x,y) 坐标处显示哪个节点的信息.我想我可以使用打开并显示图形的 pyplot 窗口,在鼠标单击时使用 (x,y) 坐标,但我需要某种图像映射来知道哪个节点已在该坐标处可视化!

I'd like my program to obtain info about which node is displayed at the (x,y) coordinate where the user clicked the graph. I guess I could work with the pyplot window that opens and displays the graph, using the (x,y) coordinates at mouse-click, but I would need some kind of imagemap to know which node has been visualized at that coordinates!

你能说它是否/如何做到吗?

Can you tell be if/how it can be done?

推荐答案

感谢 http://groups.google.com/group/networkx-discuss( http://groups.google.com/group/networkx-discuss/browse_thread/thread/aac227e1fb2a4719 ):

以下(部分)代码在 Tkinter 中工作,允许创建包含 networkx 图的 matplotlib 窗口(顺便说一下,非阻塞),并在您单击给定节点时执行过程 visitNode().

The following (partial) code works in Tkinter, allows the creation of a matplotlib window (non blocking, by the way) containing a networkx graph, and executes the procedure visitNode() if you click on a given node.

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()

这篇关于如何通过networkx制作可点击的图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆