python - pyqtgraph:如何拖动绘图项

标签 python plot qgraphicsscene pyqtgraph

目前正在尝试在 pyqtgraph 中绘制散点图并尝试拖动绘图项但无法找到方法。 已经查看了 GraphicsScene sigMouseClicked、sigMouseMoved 事件。 欢迎任何建议。 如果我需要任何进一步的详细信息,请告诉我。

我正在使用的示例代码:

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)

最佳答案

看看 pyqtgraph/examples/CustomGraphItem.py。 方法是创建一个 GraphItem 子类来捕获鼠标拖动事件并移动鼠标下方的散点图点:

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

关于python - pyqtgraph:如何拖动绘图项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22448229/

相关文章:

python - numpy.arange 除以零错误

R 直方图显示每个 bin 所花费的时间

matlab - scatter3 的问题

r - R 中的圆形堆积条形图

python - 用于预测数字输出的自定义损失函数

Python开发设置

c++ - 如何防止 QGraphicsTextItem 上的默认上下文菜单?

c++ - Qt:正确地将撤消框架与 QGraphicsScene 集成

c++ - QGraphicsItem 鼠标中键按下事件

python - 如何在 Mac 操作系统的 Rodeo GUI 中安装 xgboost?