pyqtgraph:如何拖动绘图项 [英] pyqtgraph: how to drag a a plot item

查看:34
本文介绍了pyqtgraph:如何拖动绘图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在尝试在pyqtgraph中绘制散点图并尝试拖动绘图项但无法找到方法.已经看过 GraphicsScene sigMouseClicked, sigMouseMoved 事件.欢迎任何建议.如果需要我方提供更多详细信息,请告诉我.

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.

我使用的示例代码:

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 子类来捕获鼠标拖动事件并移动鼠标下方的散点图:

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

这篇关于pyqtgraph:如何拖动绘图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆