matplotlib中的可拖动点 - 如何检索点坐标

时间:2014-05-27 13:19:07

标签: python-2.7 matplotlib coordinates draggable pyside

我希望能够沿垂直线移动一个点(matplotlib.patches.Ellipse)。为此,我在PySide QtGui.QWidget中调用了Dragable矩形类(http://matplotlib.org/1.3.1/users/event_handling.html)。

拖动我的椭圆工作正常,但是当我释放鼠标按钮时,我仍然坚持如何检索椭圆的最终y坐标(我希望在包含此按钮的绘图旁边有一个QLabel y坐标)。从on_release我有它的最终位置:

def on_release(self, event):
    if DraggablePoint.lock is not self:
        return
    self.point.newYcoordinate = self.point.center[1]
    self.press = None
    DraggablePoint.lock = None

    # draw everything but the selected point 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 point
    axes.draw_artist(self.point)

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

但是如何将其连接到我的QLabel文本(并且每次移动椭圆时都获得更新此标签)?

self.FlexibleValue = patches.Ellipse(xy=(0.5, 0.5), width=0.2, height=0.2, edgecolor='r', facecolor='r', lw=2)
self.axesResp.add_patch(self.FlexibleValue)
self.dragValue = DraggablePoint(self.FlexibleValue)
self.dragValue.connect()

1 个答案:

答案 0 :(得分:0)

通过添加tcaswell建议的信号:

def on_release(self, event):
    ...
    self.signal.updatedSignal.emit(str(self.point.newYcoordinate))

然后检索y坐标:

self.dragValue.signal.updatedSignal.connect(self.updatedValue)

使用:

def updatedValue(self, newValue):
    self.labelValue.setText(newValue)

class UpdatedSignal(QtCore.QObject):
    updatedSignal = QtCore.Signal(str)