how to track the mouse over the matplot#39;s canvas in QWidget?(如何在 QWidget 中的 matplot 画布上跟踪鼠标?)
问题描述
我想实时跟踪鼠标在 matplot 画布上的位置.
I want to track the mouse's position over a matplot's canvas in real-time.
现在,我构建了一个继承 Qwidget(像容器一样)的 MplWidget,然后在它上面构建了一个画布来显示绘图.但是,问题是我只能在除画布区域之外的填充区域中跟踪鼠标的位置.
For now, I built a MplWidget that inherits the Qwidget (act like a container), then built a canvas over it to show the plot. However, the problem is that I can only track the mouse's position in the padding area except for the canvas area.
由于我的画布继承了不是 QWidget 的 matplotlib.figure,因此它没有 setMouseTracking() 属性.这样,如何解决这个问题?
Since my canvas inherits the matplotlib.figure that is not a QWidget, thus it doesn't have the setMouseTracking() attribute. In this way, how to resolve this issue?
我发现了一个非常有用的链接如何实时返回鼠标坐标?.然而,它也面临同样的问题.当鼠标在标签(文本区域)上时,跟踪功能似乎被中断了.
I found a quite useful link How to return mouse coordinates in realtime?. However, it also suffers the same issue. When the mouse is over the label (text area), the tracking function seems to be interrupted.
我的这个类的代码如下所示:
my code for this class shown here:
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!')
推荐答案
您已经注意到画布不是由 Qt 而是由 matplotlib 处理的,因此解决方案是使用该库提供的事件,如果您查看 文档 你看到有以下事件:
As you have noticed the canvas is not handled by Qt but by matplotlib so the solution is to use the events provided by that library, if you review the docs you see that there are the following events:
事件名称 类别和描述
'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
在您的情况下,您应该使用事件:
In your case you should use the events:
- 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_())
这篇关于如何在 QWidget 中的 matplot 画布上跟踪鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 QWidget 中的 matplot 画布上跟踪鼠标?
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01