Search code examples
pythonqt5pyqtgraphqgraphicsitem

How can I make a PlotDataItem with unique IDs per distinct line segment?


I also posted in the pyqtgraph forum here.

My overall goal is to have several clickable regions overlaid on an image, and if the plot boundary of any region is clicked I get a signal with the ID of that region. Something like this: enter image description here

If I use only one PlotDataItem with nan-separated curves then each boundary sends the same signal. However, using a separate PlotDataItem for each boundary makes the application extremely sluggish.

I ended up subclassing ScatterPlotItem and rewriting the pointsAt function, which does what I want. The problem now is I can't figure out the appropriate way to change the ScatterPlotItem's boundingRect. Am I on the right approach? Is there a better way of doing this? enter image description here

import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui


class CustScatter(pg.ScatterPlotItem):
  def pointsAt(self, pos: QtCore.QPointF):
    """
    The default implementation only checks a square around each spot. However, this is not
    precise enough for my needs. It also triggers when clicking *inside* the spot boundary,
    which I don't want.
    """
    pts = []
    for spot in self.points(): # type: pg.SpotItem
      symb = QtGui.QPainterPath(spot.symbol())
      symb.translate(spot.pos())
      stroker = QtGui.QPainterPathStroker()
      mousePath = stroker.createStroke(symb)
      # Only trigger when clicking a boundary, not the inside of the shape
      if mousePath.contains(pos):
        pts.append(spot)
    return pts[::-1]

"""Make some sample data"""
tri = np.array([[0,2.3,0,1,4,5,0], [0,4,4,8,8,3,0]]).T
tris = []
xyLocs = []
datas = []
for ii in np.arange(0, 16, 5):
  curTri = tri + ii
  tris.append(curTri)
  xyLocs.append(curTri.min(0))
  datas.append(ii)

def ptsClicked(item, pts):
  print(f'ID {pts[0].data()} Clicked!')

"""Logic for making spot shapes from a list of (x,y) vertices"""
def makeSymbol(verts: np.ndarray):
  outSymbol = QtGui.QPainterPath()
  symPath = pg.arrayToQPath(*verts.T)
  outSymbol.addPath(symPath)
  # From pyqtgraph.examples for plotting text
  br = outSymbol.boundingRect()
  tr = QtGui.QTransform()
  tr.translate(-br.x(), -br.y())
  outSymbol = tr.map(outSymbol)
  return outSymbol

app = pg.mkQApp()
pg.setConfigOption('background', 'w')

symbs = []
for xyLoc, tri in zip(xyLocs, tris):
  symbs.append(makeSymbol(tri))

"""Create the scatterplot"""
xyLocs = np.vstack(xyLocs)
tri2 = pg.PlotDataItem()
scat = CustScatter(*xyLocs.T, symbol=symbs, data=datas, connect='finite',
                   pxMode=False, brush=None, pen=pg.mkPen(width=5), size=1)
scat.sigClicked.connect(ptsClicked)
# Now each 'point' is one of the triangles, hopefully

"""Construct GUI window"""
w = pg.PlotWindow()
w.plotItem.addItem(scat)
plt: pg.PlotItem = w.plotItem
plt.showGrid(True, True, 1)
w.show()
app.exec()

Solution

  • Solved! It turns out unless you specify otherwise, the boundingRect of each symbol in the dataset is assumed to be 1 and that the spot size is the limiting factor. After overriding measureSpotSizes as well my solution works:

    import numpy as np
    import pyqtgraph as pg
    from pyqtgraph.Qt import QtCore, QtGui
    
    
    class CustScatter(pg.ScatterPlotItem):
      def pointsAt(self, pos: QtCore.QPointF):
        """
        The default implementation only checks a square around each spot. However, this is not
        precise enough for my needs. It also triggers when clicking *inside* the spot boundary,
        which I don't want.
        """
        pts = []
        for spot in self.points(): # type: pg.SpotItem
          symb = QtGui.QPainterPath(spot.symbol())
          symb.translate(spot.pos())
          stroker = QtGui.QPainterPathStroker()
          mousePath = stroker.createStroke(symb)
          # Only trigger when clicking a boundary, not the inside of the shape
          if mousePath.contains(pos):
            pts.append(spot)
        return pts[::-1]
    
      def measureSpotSizes(self, dataSet):
        """
        Override the method so that it takes symbol size into account
        """
        for rec in dataSet:
          ## keep track of the maximum spot size and pixel size
          symbol, size, pen, brush = self.getSpotOpts(rec)
          br = symbol.boundingRect()
          size = max(br.width(), br.height())*2
          width = 0
          pxWidth = 0
          if self.opts['pxMode']:
            pxWidth = size + pen.widthF()
          else:
            width = size
            if pen.isCosmetic():
              pxWidth += pen.widthF()
            else:
              width += pen.widthF()
          self._maxSpotWidth = max(self._maxSpotWidth, width)
          self._maxSpotPxWidth = max(self._maxSpotPxWidth, pxWidth)
        self.bounds = [None, None]
    
    """Make some sample data"""
    tri = np.array([[0,2.3,0,1,4,5,0], [0,4,4,8,8,3,0]]).T
    tris = []
    xyLocs = []
    datas = []
    for ii in np.arange(0, 16, 5):
      curTri = tri + ii
      tris.append(curTri)
      xyLocs.append(curTri.min(0))
      datas.append(ii)
    
    def ptsClicked(item, pts):
      print(f'ID {pts[0].data()} Clicked!')
    
    """Logic for making spot shapes from a list of (x,y) vertices"""
    def makeSymbol(verts: np.ndarray):
      plotVerts = verts - verts.min(0, keepdims=True)
      symPath = pg.arrayToQPath(*plotVerts.T)
      return symPath
    
    app = pg.mkQApp()
    pg.setConfigOption('background', 'd')
    
    symbs = []
    for xyLoc, tri in zip(xyLocs, tris):
      symbs.append(makeSymbol(tri))
    
    """Create the scatterplot"""
    xyLocs = np.vstack(xyLocs)
    tri2 = pg.PlotDataItem()
    scat = CustScatter(*xyLocs.T, symbol=symbs, data=datas, connect='finite',
                       pxMode=False, brush=None, pen=pg.mkPen(width=5), size=1)
    scat.sigClicked.connect(ptsClicked)
    # Now each 'point' is one of the triangles, hopefully
    
    """Construct GUI window"""
    w = pg.PlotWindow()
    w.plotItem.addItem(scat)
    plt: pg.PlotItem = w.plotItem
    plt.showGrid(True, True, 1)
    w.show()
    app.exec()