带有 PyQtGraph 图的 PyQt5 gui:在右侧显示 y 轴

PyQt5 gui with PyQtGraph plot: Display y axis on the right

我正在创建 PyQt5 Gui,我正在使用 PyQtGraph 绘制一些数据。这是一个最小的、完整的、可验证的示例脚本,它与我的结构非常相似。

import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, QApplication)
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui

class CustomPlot(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data
        print(self.data)
        self.generatePicture()

    def generatePicture(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('w', width=1/2.))
        for (t, v) in self.data:
            p.drawLine(QtCore.QPointF(t, v-2), QtCore.QPointF(t, v+2))
        p.end()

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.simpleplot()

    def initUI(self):
        self.guiplot = pg.PlotWidget()
        layout = QGridLayout(self)
        layout.addWidget(self.guiplot, 0,0)

    def simpleplot(self):
        data = [
            (1., 10),
            (2., 13),
            (3., 17),
            (4., 14),
            (5., 13),
            (6., 15),
            (7., 11),
            (8., 16)
        ]
        pgcustom = CustomPlot(data)
        self.guiplot.addItem(pgcustom)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

这会生成一个看起来像

y 轴在图的左侧,但我想将它移到右侧。我尝试了很多方法,但找不到具有实现此目的的选项或方法的对象(QtGui.QPainter、GraphicObject 等)。

可以通过PlotItemclass的方法设置。

def initUI(self):
    self.guiplot = pg.PlotWidget()
    plotItem = self.guiplot.getPlotItem()
    plotItem.showAxis('right')
    plotItem.hideAxis('left')

如果您还没有阅读关于 Organization of Plotting Classes 的部分,请看一下。特别是 PlotWidetPlotItem class 之间的关系。