如何在QWidget中的matplot画布上跟踪鼠标?

时间:2019-05-23 22:02:22

标签: python matplotlib pyqt pyqt5

我想实时在matplot的画布上跟踪鼠标的位置。

现在,我构建了一个继承Qwidget(类似于容器)的MplWidget,然后在其上方构建了一个画布以显示该图。但是,问题是我只能在画布区域之外的填充区域中跟踪鼠标的位置。

由于我的画布继承了不是QWidget的matplotlib.figure,因此它没有setMouseTracking()属性。这样,如何解决此问题?

我找到了一个非常有用的链接How to return mouse coordinates in realtime?。但是,它也遇到相同的问题。当鼠标悬停在标签(文本区域)上时,跟踪功能似乎被中断。

我在这堂课的代码显示在这里:

from PyQt5.QtWidgets import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.figure import Figure


class MplWidget(QWidget):

    def __init__(self, parent=None):
        # QWidget.__init__(self, parent)
        super(QWidget, self).__init__(parent)

        self.canvas = FigureCanvas(Figure())

        vertical_layout = QVBoxLayout()
        vertical_layout.addWidget(self.canvas)

        self.canvas.axes = self.canvas.figure.add_subplot(111)
        self.setLayout(vertical_layout)

        self.setMouseTracking(True)

    def mouseMoveEvent(self, e):
        text = "x: {0},  y: {1}".format(e.x(), e.y())
        print(text)
        super(MplWidget, self).mouseMoveEvent(e)

    def mousePressEvent(self, e):
        print('click!')

1 个答案:

答案 0 :(得分:1)

您已经注意到,画布不是由Qt处理,而是由matplotlib处理,因此解决方案是使用该库提供的事件,如果查看the docs,则会发现存在以下事件:

  

事件名称 类别和说明

'button_press_event'  MouseEvent - mouse button is pressed
'button_release_event'    MouseEvent - mouse button is released
'draw_event'  DrawEvent - canvas draw (but before screen update)
'key_press_event' KeyEvent - key is pressed
'key_release_event'   KeyEvent - key is released
'motion_notify_event' MouseEvent - mouse motion
'pick_event'  PickEvent - an object in the canvas is selected
'resize_event'    ResizeEvent - figure canvas is resized
'scroll_event'    MouseEvent - mouse scroll wheel is rolled
'figure_enter_event'  LocationEvent - mouse enters a new figure
'figure_leave_event'  LocationEvent - mouse leaves a figure
'axes_enter_event'    LocationEvent - mouse enters a new axes
'axes_leave_event'    LocationEvent - mouse leaves an axes

您应该使用以下事件:

  • button_press_event
  • button_release_event
  • motion_notify_event

示例:

from PyQt5 import QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class MplWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MplWidget, self).__init__(parent)
        self.canvas = FigureCanvas(Figure())

        vertical_layout = QtWidgets.QVBoxLayout(self)
        vertical_layout.addWidget(self.canvas)

        self.canvas.axes = self.canvas.figure.add_subplot(111)

        self.canvas.mpl_connect("button_press_event", self.on_press)
        self.canvas.mpl_connect("button_release_event", self.on_release)
        self.canvas.mpl_connect("motion_notify_event", self.on_move)

    def on_press(self, event):
        print("press")
        print("event.xdata", event.xdata)
        print("event.ydata", event.ydata)
        print("event.inaxes", event.inaxes)
        print("x", event.x)
        print("y", event.y)

    def on_release(self, event):
        print("release:")
        print("event.xdata", event.xdata)
        print("event.ydata", event.ydata)
        print("event.inaxes", event.inaxes)
        print("x", event.x)
        print("y", event.y)

    def on_move(self, event):
        print("move")
        print("event.xdata", event.xdata)
        print("event.ydata", event.ydata)
        print("event.inaxes", event.inaxes)
        print("x", event.x)
        print("y", event.y)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MplWidget()
    w.show()
    sys.exit(app.exec_())