1

Currently trying to plot a scatter plot in pyqtgraph and trying to drag the plot items but unable to find the approach. Already looked at GraphicsScene sigMouseClicked, sigMouseMoved events. Any suggestions welcome. Let me know in case any further details are required from my side.

Sample code which I am using:

import pyqtgraph as pg
import numpy as np

w = pg.GraphicsWindow()
w.show()
x = [2,4,5,6,8];
y = [2,4,6,8,10];

pl = pg.PlotItem()
pl.plot(x, y, symbol='o')
w.addItem(pl)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Versatile
  • 459
  • 5
  • 20

1 Answers1

2

Have a look at pyqtgraph/examples/CustomGraphItem.py. The approach there is to create a GraphItem subclass that catches mouse drag events and moves the scatter plot point that is under the mouse:

def mouseDragEvent(self, ev):
    if ev.button() != QtCore.Qt.LeftButton:
        ev.ignore()
        return

    if ev.isStart():
        # We are already one step into the drag.
        # Find the point(s) at the mouse cursor when the button was first 
        # pressed:
        pos = ev.buttonDownPos()
        pts = self.scatter.pointsAt(pos)
        if len(pts) == 0:
            ev.ignore()
            return
        self.dragPoint = pts[0]
        ind = pts[0].data()[0]
        self.dragOffset = self.data['pos'][ind] - pos
    elif ev.isFinish():
        self.dragPoint = None
        return
    else:
        if self.dragPoint is None:
            ev.ignore()
            return

    ind = self.dragPoint.data()[0]
    self.data['pos'][ind] = ev.pos() + self.dragOffset
    self.updateGraph()
    ev.accept()
Luke
  • 11,374
  • 2
  • 48
  • 61
  • Thanks for the reply, but I am unable to find the relevant example under my folder. Can you please share the full example here for my understanding. – Versatile Mar 18 '14 at 06:00
  • Sorry, perhaps that wasn't released yet: https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/CustomGraphItem.py – Luke Mar 18 '14 at 13:54