如何在pyqtgraph中有效地绘制和显示轴上的对象和标签?

2024-10-01 02:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在pyqtgraph中的AxisItems顶部显示QT的图形对象。在

要想知道如何做到这一点,我想也许有一种更好的方法可以更有效地使用内置的pyqtgraph maptosene/mapToView函数。在

我最初认为这很简单,从文档(http://www.pyqtgraph.org/documentation/plotting.html#organization-of-plotting-classes)来看,这只是一个绘制对象的例子,并使用更高的Z值引用PlotItem(GraphicsItem)中的pyqtgraph坐标,应该会使对象漂浮在axisitem的顶部。在

但是,当我尝试此操作时,无论我将Z值设置得多高或选择任何对象(viewbox/plotitem/plotwidget)作为父对象,它们都会消失在轴后面。作为一个解决方法,我引用了qtGraphicscene,它确实起作用,但是坐标稍微偏离了几个像素,正如你在我的演示代码中看到的,每当鼠标移动时,这是意料之中的,因为它在pyqtgraph和Qt坐标系之间转换。在

有人能提出更好的方法吗?或者如何在演示中用精确的值排列箭头?在

from: http://www.pyqtgraph.org/documentation/plotting.html#organization-of-plotting-classes

屏幕截图和代码:

Crosshair with arrows on axis

from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QLabel, QMainWindow, QHBoxLayout, QVBoxLayout
from PyQt5.QtCore import QThread
from PyQt5.QtCore import QObject
from PyQt5.QtCore import pyqtSlot, pyqtSignal

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
from mainwindowgraph import Ui_MainWindow as QTestGraphMainWindow 
import sys

class TestGraphWidget(QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):               
        self.ui = QTestGraphMainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle('Test Graphwindow')
        #This line is required to get the corect  viewbox
        self.vb = self.ui.mainplot.plotItem.vb

        proxy = pg.SignalProxy(self.ui.mainplot.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
        self.ui.mainplot.scene().sigMouseMoved.connect(self.mouseMoved)

        #cross hair
        self.vLine = pg.InfiniteLine(angle=90, movable=False)
        self.hLine = pg.InfiniteLine(angle=0, movable=False)
        self.vLine.setZValue(2)
        self.hLine.setZValue(2)
        self.ui.mainplot.addItem(self.vLine, ignoreBounds=True)
        self.ui.mainplot.addItem(self.hLine, ignoreBounds=True)
        self.h_arrow = pg.ArrowItem(angle=0)
        self.v_arrow = pg.ArrowItem(angle=90)

        self.ui.mainplot.scene().addItem(self.v_arrow)
        self.ui.mainplot.scene().addItem(self.h_arrow)

        self.h_arrow.setZValue(10)
        self.v_arrow.setZValue(10)
        self.ui.mainplot.addItem(pg.PlotCurveItem([99,20,40,31,40,1], pen='b'))

    def mouseMoved(self, evt):
        pos = evt
        if self.ui.mainplot.sceneBoundingRect().contains(pos):
            mousePoint = self.vb.mapSceneToView(pos)
            #TODO - link  to axisitem instead of plotItemvb  <ViewBox>
            axis_offset = 0
            rightaxis_x = self.ui.mainplot.plotItem.vb.x() + self.ui.mainplot.plotItem.vb.width() + axis_offset 
            bottomaxis_y = self.ui.mainplot.plotItem.vb.y() + self.ui.mainplot.plotItem.vb.height() + axis_offset 
            index_x = mousePoint.x()
            print('x= {}, y = {}'.format(mousePoint.x() , mousePoint.y()))
            self.vLine.setPos(index_x)
            self.hLine.setPos(mousePoint.y())
            self.h_arrow.setPos(rightaxis_x, pos.y())
            self.v_arrow.setPos(pos.x(), bottomaxis_y)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    testgraphwindow = TestGraphWidget()
    testgraphwindow.ui.mainplot.showAxis('right')
    testgraphwindow.ui.mainplot.hideAxis('left')
    testgraphwindow.show()
    sys.exit(app.exec_())

在主窗口图形.py公司名称:

^{pr2}$

Tags: 对象fromposimportselfuipyqt5pyqtgraph