pyqtgraph: how to drag a a plot item

柔情痞子 提交于 2020-01-14 03:52:07

问题


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)

回答1:


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()


来源:https://stackoverflow.com/questions/22448229/pyqtgraph-how-to-drag-a-a-plot-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!