如何在单击鼠标的位置绘制并填充 PyQtGraph 图中的矩形

How to draw and fill a rectangle in a PyQtGraph plot at position where mouse is clicked

我正在使用 PyQt5 和 PyQtGraph。我简化了下面的示例代码。然后我想在每次点击鼠标的位置在plot view中绘制一个红色的小矩形,从而在plot view中累积了几个红色矩形。下面的代码有一个#???在我需要一些有关绘制红色矩形的代码方面的帮助的地方发表评论。

import sys
from PyQt5 import QtWidgets
import numpy as np
import pyqtgraph as pg
from pyqtgraph import PlotWidget, plot

# *********************************************************************************************
# *********************************************************************************************

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setWindowTitle("My MainWindow")
        self.qPlotWidget = pg.PlotWidget(self)
        self.qPlotWidget.setLabel("bottom", "X-Axis")
        self.qPlotWidget.setLabel("left", "Y-Axis")
        self.qPlotWidget.scene().sigMouseClicked.connect(self.mouseClickedEvent)

        data1 = np.zeros((2, 2), float) # create the array to hold the data
        data1[0] = np.array((1.0, 10.0))
        data1[1] = np.array((2.0, 20.0))

        pen1 = pg.mkPen(color=(255,0,0), width=1) # red
        self.qPlotWidget.plot(data1, pen=pen1, name="data1")

    def mouseClickedEvent(self, event):
        print("mouseClickedEvent")
        pos = event.scenePos()
        if (self.qPlotWidget.sceneBoundingRect().contains(pos)):
            mousePoint = self.qPlotWidget.plotItem.vb.mapSceneToView(pos)
            print("mousePoint=", mousePoint)

            # draw and fill a 2-pixel by 2-pixel red rectangle where
            # the mouse was clicked at [mousePoint.x(), mousePoint.y()]
            # ??? add code here

    def resizeEvent(self, event):
        size = self.geometry()
        self.qPlotWidget.setGeometry(10, 10, size.width()-20, size.height()-20)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    screen = QtWidgets.QDesktopWidget().screenGeometry()
    w.setGeometry(100, 100, screen.width()-200, screen.height()-200) # x, y, Width, Height
    w.show()

    sys.exit(app.exec_())

您可以做的是创建一个空的散点图项并将其添加到 self.qPlotWidget。然后在 mousrClickedEvent 中,您可以将鼠标位置添加到此散点图项目的点列表中,即

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        .... as before ....

        # add empty scatter plot item with a red brush and a square as the symbol to plot widget
        brush = pg.mkBrush(color=(255,0,0))
        self.scatterItem = pg.ScatterPlotItem(pen=None, size=10, brush=brush, symbol='s')
        self.qPlotWidget.addItem(self.scatterItem)


    def mouseClickedEvent(self, event):
        pos = event.scenePos()
        if (self.qPlotWidget.sceneBoundingRect().contains(pos)):
            mousePoint = self.qPlotWidget.plotItem.vb.mapSceneToView(pos)

            # add point to scatter item
            self.scatterItem.addPoints([mousePoint.x()], [mousePoint.y()])