如何在散点图上绘制鼠标位置?

时间:2019-08-30 01:07:18

标签: python pyqt pyqtgraph

我正在尝试创建一个多边形区域以选择散点图上的点。问题是我不知道如何在散点图中绘制鼠标位置。

我使用mapFromDevice,但是结果不令人满意,因为位置是圆形的并且不精确(我猜这是因为它适应于散点图坐标系)。

enter image description here

我不知道我是否使用了错误的方法。

from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
import pyqtgraph as pg
import sys

class MVAQPlot(QWidget):

    def __init__(self):
        QWidget.__init__(self)
        self.resize(600,600)
        pg.setConfigOption('background', 'w')
        pg.setConfigOption('foreground', 'k')

        self._graphics_layout = pg.GraphicsLayoutWidget(self)
        layout = QHBoxLayout()
        layout.addWidget(self._graphics_layout)
        self.setLayout(layout)

        self._scores = ([1, 1.5, 3, 5, 5], [1, 1.5, 3.4, 4, 2])

        self._roi = pg.PolyLineROI([], pen=(5))
        self._scatter_plot = pg.ScatterPlotItem()
        self._scatter_plot.setData(self._scores[0], self._scores[1])
        plot = self._graphics_layout.addPlot()
        plot.addItem(self._scatter_plot)
        plot.addItem(self._roi)

        self._selection_timer = QtCore.QTimer()
        self._selection_timer.timeout.connect(self.update_selection)

        self._creating_region = False
        self._last_point = None

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            if self._creating_region:
                self._creating_region = False
                self._selection_timer.stop()
                roi_shape = self._roi.mapToItem(self._scatter_plot, self._roi.shape())
                self._points = list()
                for i in range(len(self._scores[0])):
                    self._points.append(QtCore.QPoint(self._scores[0][i], self._scores[1][i]))
                selected = [roi_shape.contains(pt) for pt in self._points]
                print('Selected points: ' + str(selected))
                self._roi.clearPoints()
            else:
                self._creating_region = True
                self._selection_points = list()
                self._selection_timer.start(1000)

    def update_selection(self):
        if self._roi is not None:
            point = QtCore.QPoint(self._graphics_layout.lastMousePos[0],
                                  self._graphics_layout.lastMousePos[1])
            self._selection_points.append(point)
            region = QtGui.QPolygon(self._selection_points)
            mapped_point = self._scatter_plot.mapFromDevice(region)
            self._roi.setPoints(mapped_point)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MVAQPlot()
    widget.show()
    sys.exit(app.exec_())

我不知道如何在散点图上绘制精确的PolyLineROI。像这样:

enter image description here

1 个答案:

答案 0 :(得分:1)

问题是您使用的QPointQPolygon仅支持整数,如果传递浮点数,则将其四舍五入。解决方案是使用QPointFQPolygonF

def update_selection(self):
    if self._roi is not None:
        point = QtCore.QPointF(
            self._graphics_layout.lastMousePos[0],
            self._graphics_layout.lastMousePos[1],
        )
        self._selection_points.append(point)
        region = QtGui.QPolygonF(self._selection_points)
        mapped_point = self._scatter_plot.mapFromDevice(region)
        self._roi.setPoints(mapped_point)