Search code examples
python-2.7pyqtpyqtgraph

Unequal sizes for subplots in pyqtgraph


I have tried to get at the problem of figuring out setting different size ratios for subplots in a pyqtgraph layout with no success. Here's how the code looks

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pyqtgraph.exporters
import numpy as np

app = QtGui.QApplication([])
view = pg.GraphicsView()
l = pg.GraphicsLayout(border=(100,100,100))
view.setCentralItem(l)
view.show()
view.setWindowTitle('pyqtgraph example: GraphicsLayout')
view.resize(1000,1600)

rows=range(3)
cols=range(3)
ar=0
for row in rows:
    ac=0
    for col in cols:
        l2=l.addLayout()
        p_res = l2.addPlot()
        p_res.hideAxis('bottom')
        l2.nextRow()
        p_data = l2.addPlot()
        p_res.plot([1,1,2,2,1,1])
        p_data.plot([1,3,2,4,3,5])
        ac=ac+1
        l.nextColumn()
    l.nextRow()
    ar=ar+1


if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

exporter = pg.exporters.ImageExporter(view.scene())

# save to file
exporter.export('fileName.png')

The output from this code is as follows:

6 panelled plot

Here we see 9 plots across 3 rows and 3 columns. Considering the plot at row 1 and column 1, how is it possible to alter the ratio of the subplot size in the top panel to that in the lower panel to 1:3?


Solution

  • I had the same problem, a nice discussion and fix here https://groups.google.com/forum/#!topic/pyqtgraph/aMaCMOrrrJg, or https://groups.google.com/forum/#!topic/pyqtgraph/RJjtJea9KFc

    my code is

    self.graphics_layout_widget = pg.GraphicsLayoutWidget()  # contains a graphicsview
    self.plot_holding_grid.addWidget(self.graphics_layout_widget, 0, 0, 1, 1)
    self.graphics_layout_widget.setBackground('w')
    
    upperplot = self.graphics_layout_widget.addPlot(0, 0, 1, 1)
    plotpen = pg.mkPen(color='k', width=2)
    upperplot.plot(first_time, first_record, pen=plotpen)
    lowerplot = self.graphics_layout_widget.addPlot(1, 0, 1, 1)
    
    # restrict size of plot areas
    self.graphics_layout_widget.ci.layout.setRowStretchFactor(0, 4)
    self.graphics_layout_widget.ci.layout.setRowStretchFactor(1, 1)