Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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 如何在matploblib中返回鼠标事件坐标_Python_Matplotlib_Interactive - Fatal编程技术网

Python 如何在matploblib中返回鼠标事件坐标

Python 如何在matploblib中返回鼠标事件坐标,python,matplotlib,interactive,Python,Matplotlib,Interactive,在我的代码中,我定义了一个DragablePoint类,它支持交互式拖放功能。我还想记录并返回可拖动点的新位置。我可以在事件处理函数中获取它们(通过print),但如何在main函数中返回值?谢谢你的建议 import matplotlib.pyplot as plt import matplotlib.patches as patches import math class DraggablePoint: lock = None #only one can be animated a

在我的代码中,我定义了一个DragablePoint类,它支持交互式拖放功能。我还想记录并返回可拖动点的新位置。我可以在事件处理函数中获取它们(通过
print
),但如何在main函数中返回值?谢谢你的建议

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import math

class DraggablePoint:
    lock = None #only one can be animated at a time
    def __init__(self, point):
        self.point = point
        self.press = None
        self.background = None
        self.ax = self.point.figure.add_subplot(111)
        self.final_point = (0.0, 0.0)

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.cidrelease = self.point.figure.canvas.mpl_connect('button_release_event', self.on_release)
        self.cidmotion = self.point.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)

    def on_press(self, event):
        if event.inaxes != self.point.axes: return
        if DraggablePoint.lock is not None: return
        contains, attrd = self.point.contains(event)
        if not contains: return
        self.press = (self.point.center), event.xdata, event.ydata
        DraggablePoint.lock = self
        print (event.xdata, event.ydata)
        # draw everything but the selected rectangle and store the pixel buffer
        canvas = self.point.figure.canvas
        axes = self.point.axes
        self.point.set_animated(True)
        canvas.draw()
        self.background = canvas.copy_from_bbox(self.point.axes.bbox)

        # now redraw just the rectangle
        axes.draw_artist(self.point)

        # and blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_motion(self, event):
        if DraggablePoint.lock is not self:
            return
        if event.inaxes != self.point.axes: 
            return
        if event.xdata < event.ydata / math.sqrt(3): 
            return
        if event.xdata > (event.ydata - math.sqrt(3))/(-math.sqrt(3)):
            return
        self.point.center, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.point.center = (self.point.center[0]+dx, self.point.center[1]+dy)

        canvas = self.point.figure.canvas
        axes = self.point.axes
        # restore the background region
        canvas.restore_region(self.background)

        # redraw just the current rectangle
        axes.draw_artist(self.point)

        # blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_release(self, event):
        'on release we reset the press data'
        if DraggablePoint.lock is not self:
            return

        self.press = None
        DraggablePoint.lock = None

        # turn off the rect animation property and reset the background
        self.point.set_animated(False)
        self.background = None
        self.final_point = (event.xdata, event.ydata)
        print (event.xdata, event.ydata)
        # redraw the full figure
        self.point.figure.canvas.draw()


    def disconnect(self):
        'disconnect all the stored connection ids'
        self.point.figure.canvas.mpl_disconnect(self.cidpress)
        self.point.figure.canvas.mpl_disconnect(self.cidrelease)
        self.point.figure.canvas.mpl_disconnect(self.cidmotion)

if __name__ == "__main__":
    fig = plt.figure()
    ax = fig.add_subplot(111)
    point = (0.5, 0.5)
    circle = patches.Circle(point, 0.02, fc='b', alpha=0.5, picker=True)
    draggables = []
    ax.add_patch(circle)
    dr = DraggablePoint(circle)
    dr.connect()
    draggables.append(dr)
    fig.canvas.draw()
    print dr.final_point #this doesn't work
    plt.show()
导入matplotlib.pyplot作为plt
将matplotlib.patches导入为修补程序
输入数学
类可拖动点:
lock=None#一次只能设置一个动画
定义初始值(自身,点):
self.point=点
self.press=无
self.background=None
self.ax=self.point.figure.add_子图(111)
self.final_point=(0.0,0.0)
def连接(自):
'连接到我们需要的所有活动'
self.cidpress=self.point.figure.canvas.mpl\u connect('button\u press\u event',self.on\u press)
self.cidlease=self.point.figure.canvas.mpl\u connect('button\u release\u event',self.on\u release)
self.cidmotion=self.point.figure.canvas.mpl\u connect('motion\u notify\u event',self.on\u motion)
def on_按下(自身,事件):
如果event.inaxes!=self.point.axes:返回
如果DraggablePoint.lock不是None:返回
contains,attrd=self.point.contains(事件)
如果不包含:返回
self.press=(self.point.center)、event.xdata、event.ydata
DraggablePoint.lock=self
打印(event.xdata、event.ydata)
#绘制除选定矩形外的所有内容并存储像素缓冲区
canvas=self.point.figure.canvas
轴=自点轴
self.point.set_动画(真)
canvas.draw()
self.background=canvas.copy_from_bbox(self.point.axes.bbox)
#现在只需重新绘制矩形
axes.draw_艺术家(自身点)
#只需要重新绘制的区域
canvas.blit(axes.bbox)
def on_运动(自身、事件):
如果DraggablePoint.lock不是self:
返回
如果event.inaxes!=self.point.axes:
返回
如果event.xdata(event.ydata-math.sqrt(3))/(-math.sqrt(3)):
返回
self.point.center、xpress、ypress=self.press
dx=event.xdata-xpress
dy=event.ydata-ypress
self.point.center=(self.point.center[0]+dx,self.point.center[1]+dy)
canvas=self.point.figure.canvas
轴=自点轴
#恢复背景区域
canvas.restore\u区域(self.background)
#只重绘当前矩形
axes.draw_艺术家(自身点)
#B只是重新绘制的区域
canvas.blit(axes.bbox)
def on_释放(自身、事件):
“发布时,我们重置新闻数据”
如果DraggablePoint.lock不是self:
返回
self.press=无
DraggablePoint.lock=无
#关闭rect动画属性并重置背景
self.point.set_动画(False)
self.background=None
self.final_point=(event.xdata,event.ydata)
打印(event.xdata、event.ydata)
#重画完整的数字
self.point.figure.canvas.draw()
def断开(自):
'断开所有存储的连接ID'
self.point.figure.canvas.mpl\u断开连接(self.cidpress)
self.point.figure.canvas.mpl\u断开连接(self.cidrease)
self.point.figure.canvas.mpl\u断开连接(self.cidmotion)
如果名称=“\uuuuu main\uuuuuuuu”:
图=plt.图()
ax=图添加_子批次(111)
点=(0.5,0.5)
圆=面片。圆(点,0.02,fc='b',alpha=0.5,选取器=True)
拖动表=[]
ax.添加补片(圆形)
dr=牵引点(圆)
connect博士()
DragTables.append(dr)
图canvas.draw()
打印dr.final_point,这不起作用
plt.show()

返回主功能是什么意思?这里没有主要功能。。您不想对x\y数据做什么?新的x/y数据将在其他模块中用于执行某些操作,这就是为什么我希望将值传递给外部函数的原因,例如这里的
\uuuuuuu main\uuuu
方法。
\uuuu main\uuuu
不是方法。当文件直接运行而不是通过导入运行时,它只是
\uuuu name\uuuu
的值。好吧,你说得对,但我的问题是如何获得新的坐标值<代码>打印dr.final_point在这里不起作用。假设有另一个类A包含DragablePoint B,当我移动B时,如何获得类A中B的新坐标值?