1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[pyqtgraph] グラフのサイズ比率を設定する

Last updated at Posted at 2021-01-09

やりたいこと

Ok.png 複数グラフを並べている時のサイズ比率を設定したい

環境

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

コード

プロット用にnumpyを使用しています。 pip install numpy

"""グラフのサイズ比率を設定する"""

import dataclasses
import itertools
from typing import Optional
import sys

import numpy as np
from PyQt5 import QtWidgets  # ほぼ使わない
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):
    """メイン画面
    Attributes #
    ----------
    parent: Optional[QtWidgets.QWidget] default=None
        親画面
    """
    parent: Optional[QtWidgets.QWidget] = None

    def __post_init__(self) -> None:
        """スーパークラス読み込みとプロット追加"""
        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()

詳細

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

pg.GraphicsLayoutWidget.ci.layout.setColumnStretchFactor(col, size)
col -> カラムのインデックス
size -> 比率

行に対して変える時は

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

です

その他

複数のカラムを跨がる
multi_row.png

"""複数のカラムを跨がる"""

import dataclasses
import itertools
from typing import Optional
import sys

import numpy as np
from PyQt5 import QtWidgets  # ほぼ使わない
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):
    """メイン画面
    Attributes #
    ----------
    parent: Optional[QtWidgets.QWidget] default=None
        親画面
    """
    parent: Optional[QtWidgets.QWidget] = None

    def __post_init__(self) -> None:
        """スーパークラス読み込みとプロット追加"""
        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)
colspanで設定

参考

忘れてしまった、、

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?