【问题标题】:PyQt5 gui with PyQtGraph plot: Display y axis on the rightPyQt5 gui 与 PyQtGraph 图:在右侧显示 y 轴
【发布时间】:2018-01-29 17:28:27
【问题描述】:

我正在创建一个 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 等)。

【问题讨论】:

    标签: python-3.x pyqt5 pyqtgraph


    【解决方案1】:

    可以通过PlotItem类的方法设置。

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

    如果您还没有阅读Organization of Plotting Classes 上的部分,请查看它。特别是PlotWidet 和PlotItem 类之间的关系。

    【讨论】:

    • 感谢您的提示。我浏览了 PyQt5 教程并阅读了 PyQtGraph 的相关文档,但错过了您提到的部分,
    猜你喜欢
    • 1970-01-01
    • 2022-12-11
    • 2015-06-10
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2012-05-08
    • 2020-05-11
    相关资源
    最近更新 更多