python - onclick方法,捕获坐标荧光笔出现matplotlib

标签 python matplotlib wxpython

我有一个散点图,并通过 matplotlib http://matplotlib.org/users/transforms_tutorial.html#axes-coordinates 实现了荧光笔类型的东西.

目前,当您单击任何点时,荧光笔会出现在同一位置,但我想要做的是,当您单击某个点时,它会获取该点的坐标并突出显示该点所在的位置。 我的代码是这样的

def onclick    
    ind = event.ind
    # the x coords of this transformation are data, and the
    # y coord are axes
    trans = transforms.blended_transform_factory(
    ax.transData, ax.transAxes)

    # highlight the 1..2 stddev region with a span.
    # We want x to be in data coordinates and y to
    # span from 0..1 in axes coords
    rect = patches.Rectangle((1,0), width=1, height=1,
                     transform=trans, color='yellow',
                     alpha=0.5)

    ax.add_patch(rect)
    print ('on pick scatter:' , ind , np.take(x,ind), np.take(y,ind))

最佳答案

可能有一种更短的方法可以做到这一点,但这是我的尝试并且它有效:)

    def onpick3(event):
            ind = event.ind
    l = np.take(x,ind)
        for i in range(0,1):
                d = l[i]



    if highlight == 1 :

        global highlight
        highlight = 0;
        self.canvas.draw()
            if highlight == 0 :

            trans = transforms.blended_transform_factory(self.axes.transData, self.axes.transAxes)
                rect = patches.Rectangle(((d-10),0),width=25, height=25, transform=trans, color='yellow',alpha=0.5)
                self.axes.add_patch(rect)
            self.canvas.draw()
        rect.remove()
        else:
         self.canvas.draw()
         trans = transforms.blended_transform_factory(self.axes.transData, self.axes.transAxes)
             rect = patches.Rectangle(((d-10),0),width=25, height=25, transform=trans, color='yellow',alpha=0.5)
             self.axes.add_patch(rect)
         self.canvas.draw()
         highlight = 1
         rect.remove()
            print ('onpick3 scatter:', ind, np.take(x, ind), np.take(y, ind))

关于python - onclick方法,捕获坐标荧光笔出现matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14673195/

相关文章:

python - Axes3D 数据标签和绘图限制

wxpython - Wx.choice 在 ubuntu 中不工作

python - 如何遍历所有 wx.CheckBox 实例?

python : is it ok returning both boolean and string?

python - 将 Spark DataFrame 写入 Hive 表时的内存分配问题

python - 文本转列功能

python - 没有转置数据帧的 Pandas 线图

python - 我需要帮助从内存中发送 Excel 附件 Mailgun

python - 是否可以在 matplotlib 中使用 set_xticklabels 格式化标签?

python - WxPython、Windows Vista 64 位和失败