如何在 Pyqtgraph 中设置图表的拉伸因子?

Posted

技术标签:

【中文标题】如何在 Pyqtgraph 中设置图表的拉伸因子?【英文标题】:How set stretch factor for chart in Pyqtgraph? 【发布时间】:2021-01-31 18:54:55 【问题描述】:

现在应用程序显示两个高度相同的图形:

代码:

import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QListWidget, QVBoxLayout, QMainWindow
import pyqtgraph as pg


class Draw_interface(QMainWindow):
    def __init__(self):
        global time_update

        super(Draw_interface, self).__init__()
        uic.loadUi('charts.ui', self)

        # Add two charts
        self.my_draw = pg.GraphicsLayoutWidget(show=True)
        self.p1 = self.my_draw.addPlot(row=0, col=0, stretch=3)
        self.p2 = self.my_draw.addPlot(row=1, col=0, stretch=1)

        # Set widget for chart
        my_layout = QVBoxLayout()
        my_layout.addWidget(self.my_draw)
        self.frame_for_charts.setLayout(my_layout)

        # Draw charts
        y = [2.2, 3.0, 1.3, 2.5, 1.9, 2.2, 5.5, 6.6]
        y2 = [2.3, 3.3, 2.8, 2.2, 3.3, 3.1, 2.8, 4.4]
        curve1 = self.p1.plot(y)
        curve2 = self.p2.plot(y2)

        self.show()

my_app = QtWidgets.QApplication([])
my_main_window = Draw_interface()
sys.exit(my_app.exec_())

stretch=3stratch=1 - 不会导致预期的效果。

如何正确设置拉伸系数,使上图为高度的75%,下图为高度的25%? 就像这样:

也许:

self.my_draw.itemAt(0, 0).setGeometry(x1,y1,x2,y2)
self.my_draw.itemAt(0, 0).updateGeometry()

但这显然不是最好的选择。

【问题讨论】:

【参考方案1】:

addPlot 方法没有像“stretch”或“stratch”这样的参数。

如果你想使用GraphicsLayoutWidget,你可以添加空标签来占据下一列的空间作为网格的参考。(有点hacky...)

或者您可以在QVBoxLayout 中放入2 个plotwidget,并在addWidgets 方法中使用“stretch”参数。

使用QVBoxLayout的结果更准确。

GraphicsLayoutWidget 解决方案。

import sys
from PyQt5 import QtWidgets, QtCore
import pyqtgraph as pg


class Draw_interface(QtWidgets.QMainWindow):
    def __init__(self):

        super(Draw_interface, self).__init__()
  
        # Add two charts
        self.my_draw = pg.GraphicsLayoutWidget(show=True)
        self.p1 = self.my_draw.addPlot(row=0, col=0, rowspan = 3)
        self.p2 = self.my_draw.addPlot(row=3, col=0)

        # Add label with no text to take up spaces of the next column 
        self.p3 = self.my_draw.addLabel(text='',row=0, col=1)
        self.p4 = self.my_draw.addLabel(text='',row=1, col=1)
        self.p5 = self.my_draw.addLabel(text='',row=2, col=1)
        self.p6 = self.my_draw.addLabel(text='',row=3, col=1)
        
        # Draw charts
        y = [2.2, 3.0, 1.3, 2.5, 1.9, 2.2, 5.5, 6.6]
        y2 = [2.3, 3.3, 2.8, 2.2, 3.3, 3.1, 2.8, 4.4]
        self.p1.plot(y)
        self.p2.plot(y2)
        self.setCentralWidget(self.my_draw)
        
    
def main():
    QtWidgets.QApplication.setAttribute(QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    app = QtWidgets.QApplication(sys.argv)
    
    main = Draw_interface()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

QVBoxLayout 解决方案。

import sys
from PyQt5 import QtWidgets, QtCore
import pyqtgraph as pg


class Draw_interface(QtWidgets.QMainWindow):
    def __init__(self):

        super(Draw_interface, self).__init__()
  
        # Add two charts
        p1 = pg.PlotWidget(name = "Plot1")
        p2 = pg.PlotWidget(name = "Plot2")
        
        # Draw charts
        y = [2.2, 3.0, 1.3, 2.5, 1.9, 2.2, 5.5, 6.6]
        y2 = [2.3, 3.3, 2.8, 2.2, 3.3, 3.1, 2.8, 4.4]
        p1.plot(y)
        p2.plot(y2)

        l = QtWidgets.QVBoxLayout()
        l.addWidget(p1, stretch=3)
        l.addWidget(p2, stretch=1)

        w = QtWidgets.QWidget()
        w.setLayout(l)
        self.setCentralWidget(w)
        self.setStyleSheet("QWidget  background-color: black; ")
    
def main():
    QtWidgets.QApplication.setAttribute(QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    app = QtWidgets.QApplication(sys.argv)
    
    main = Draw_interface()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

【讨论】:

以上是关于如何在 Pyqtgraph 中设置图表的拉伸因子?的主要内容,如果未能解决你的问题,请参考以下文章

拉伸因子在 Qt 中是如何工作的?

尝试在 SurfaceView 中设置时拉伸相机预览

在 PyQtGraph GraphicsWindow 中设置图像的对比度

如何在 android listview 中设置页脚以动态拉伸到底部?

如何将鼠标点击信号连接到 pyqtgraph 绘图小部件

PyQt4 中的 QThreading PyQtGraph PlotWidgets