[PYTHON] [pyqtgraph] Set the size ratio of the graph

Thing you want to do

Ok.png I want to set the size ratio when arranging multiple graphs

environment

Mac OS Python 3.8.5

PyQt5 5.15.2 PyQt5-sip 12.8.1 pyqtgraph 0.11.1

pip install PyQt5 PyQt5-sip pyqtgraph

code

I'm using numpy for plotting. pip install numpy

"""Set the size ratio of the graph"""

import dataclasses
import itertools
from typing import Optional
import sys

import numpy as np
from PyQt5 import QtWidgets  #Almost never used
import pyqtgraph as pg

SAMPLE_DATA1 = np.linspace(-100, 100) ** 1
SAMPLE_DATA2 = np.linspace(-100, 100) ** 2
SAMPLE_DATA3 = np.linspace(-100, 100) ** 3


@dataclasses.dataclass
class ChangeSizeRatioWidget(pg.GraphicsLayoutWidget):
    """Main screen
    Attributes #
    ----------
    parent: Optional[QtWidgets.QWidget] default=None
Parent screen
    """
    parent: Optional[QtWidgets.QWidget] = None

    def __post_init__(self) -> None:
        """Load superclass and add plot"""
        super(ChangeSizeRatioWidget, self).__init__(parent=self.parent)

        self.plotter1 = self.addPlot(row=0, col=0)
        self.plotter1.showGrid(x=True, y=True, alpha=0.8)
        self.plotter1_curve = self.plotter1.plot(pen=pg.mkPen('#f00', width=5))
        self.plotter1_curve.setData(SAMPLE_DATA1)

        self.plotter2 = self.addPlot(row=0, col=1)
        self.plotter2.showGrid(x=True, y=True, alpha=0.8)
        self.plotter2_curve = self.plotter2.plot(pen=pg.mkPen('#00f', width=5))
        self.plotter2_curve.setData(SAMPLE_DATA2)

        self.ci.layout.setColumnStretchFactor(0, 8)
        self.ci.layout.setColumnStretchFactor(1, 5)


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = ChangeSizeRatioWidget(parent=None)
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

Details

self.ci.layout.setColumnStretchFactor(0, 8)
self.ci.layout.setColumnStretchFactor(1, 5)

pg.GraphicsLayoutWidget.ci.layout.setColumnStretchFactor(col, size) col-> column index size-> ratio

When changing for a line

self.ci.layout.setRowStretchFactor(0, 8)
self.ci.layout.setRowStretchFactor(1, 5)

is

Other

Straddle multiple columns multi_row.png

"""Straddle multiple columns"""

import dataclasses
import itertools
from typing import Optional
import sys

import numpy as np
from PyQt5 import QtWidgets  #Almost never used
import pyqtgraph as pg

SAMPLE_DATA1 = np.linspace(-100, 100) ** 1
SAMPLE_DATA2 = np.linspace(-100, 100) ** 2
SAMPLE_DATA3 = np.linspace(-100, 100) ** 3


@dataclasses.dataclass
class ChangeSizeRatioWidget(pg.GraphicsLayoutWidget):
    """Main screen
    Attributes #
    ----------
    parent: Optional[QtWidgets.QWidget] default=None
Parent screen
    """
    parent: Optional[QtWidgets.QWidget] = None

    def __post_init__(self) -> None:
        """Load superclass and add plot"""
        super(ChangeSizeRatioWidget, self).__init__(parent=self.parent)

        self.plotter1 = self.addPlot(row=0, col=0)
        self.plotter1.showGrid(x=True, y=True, alpha=0.8)
        self.plotter1_curve = self.plotter1.plot(pen=pg.mkPen('#f00', width=5))
        self.plotter1_curve.setData(SAMPLE_DATA1)

        self.plotter2 = self.addPlot(row=0, col=1)
        self.plotter2.showGrid(x=True, y=True, alpha=0.8)
        self.plotter2_curve = self.plotter2.plot(pen=pg.mkPen('#00f', width=5))
        self.plotter2_curve.setData(SAMPLE_DATA2)

        self.plotter3 = self.addPlot(row=1, col=0, colspan=2)
        self.plotter3.showGrid(x=True, y=True, alpha=0.8)
        self.plotter3_curve = self.plotter3.plot(pen=pg.mkPen('#0f0', width=5))
        self.plotter3_curve.setData(SAMPLE_DATA3)


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = ChangeSizeRatioWidget(parent=None)
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

self.plotter3 = self.addPlot(row=1, col=0, colspan=2) Set with colspan

reference

I forgot,,

Recommended Posts

[pyqtgraph] Set the size ratio of the graph
Increase the font size of the graph with matplotlib
Connected components of the graph
Increase the UI size of MyPaint
About the size of matplotlib points
Align the size of the colorbar with matplotlib
Display the graph of tensorBoard on jupyter
Tips: Comparison of the size of three values
Set the process name of the Python program
Graph the ratio of topcoder, Codeforces and TOEIC by rating (Pandas + seaborn)
Change the font size of the legend in df.plot
Set the range of active strips to the preview range
The basis of graph theory with matplotlib animation
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
The background color of the QWidget subclass cannot be set
relation of the Fibonacci number series and the Golden ratio
Omit the decimal point of the graph scale in matplotlib
Save the graph drawn by pyqtgraph to an image
Get the size (number of elements) of UnionFind in Python
[Blender] How to dynamically set the selection of EnumProperty
Set the specified column of QTableWidget to ReadOnly StyledItemDelegate
Adjust the ratio of multiple figures with the matplotlib gridspec
The beginning of cif2cell
The meaning of self
the zen of Python
Reduction of slug size
The story of sys.path.append ()
Set function of NumPy
Revenge of the Types: Revenge of types
[pyqtgraph] Add region to the graph and link it with the graph region
Have the equation graph of the linear function drawn in Python
Find the diameter of the graph by breadth-first search (Python memory)
Set information such as length on the edge of NetworkX