[PYTHON] [pyqtgraph] Link Plot Options-> Transforms display

Thing you want to do

Right-click inside the pyqt graph and select Plot Options-> Transforms to change the display format of the graph. describe.gif

Types of display changes transforms.png

The purpose is to link this change in multiple graphs. link.gif

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

"""pyqtgraph Plot Options->Link Transforms"""

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) ** 2
SAMPLE_DATA2 = np.linspace(-100, 100) ** 3


@dataclasses.dataclass
class GraphLinkWidget(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(GraphLinkWidget, 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))  #Line color:Red,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.connect_slot()

    def connect_slot(self) -> None:
        """Slot connection
        itertools.permutations is a function that creates permutations
        ex) list(itertools.permutations([1, 2, 3]))
        >> [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
        """
        for plot1, plot2 in itertools.permutations([self.plotter1, self.plotter2]):
            plot1.ctrl.fftCheck.toggled.connect(plot2.ctrl.fftCheck.setChecked)
            plot1.ctrl.logXCheck.toggled.connect(plot2.ctrl.logXCheck.setChecked)
            plot1.ctrl.logYCheck.toggled.connect(plot2.ctrl.logYCheck.setChecked)
            plot1.ctrl.derivativeCheck.toggled.connect(plot2.ctrl.derivativeCheck.setChecked)
            plot1.ctrl.phasemapCheck.toggled.connect(plot2.ctrl.phasemapCheck.setChecked)


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


if __name__ == "__main__":
    main()

Details

Linked below

    def connect_slot(self) -> None:
        """Slot connection
        itertools.permutations is a function that creates permutations
        ex) list(itertools.permutations([1, 2, 3]))
        >> [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
        """
        for plot1, plot2 in itertools.permutations([self.plotter1, self.plotter2]):
            plot1.ctrl.fftCheck.toggled.connect(plot2.ctrl.fftCheck.setChecked)
            plot1.ctrl.logXCheck.toggled.connect(plot2.ctrl.logXCheck.setChecked)
            plot1.ctrl.logYCheck.toggled.connect(plot2.ctrl.logYCheck.setChecked)
            plot1.ctrl.derivativeCheck.toggled.connect(plot2.ctrl.derivativeCheck.setChecked)
            plot1.ctrl.phasemapCheck.toggled.connect(plot2.ctrl.phasemapCheck.setChecked)

plot1.ctrl.logXCheck.toggled.connect(plot2.ctrl.logXCheck.setChecked) For example, in the above case, when the logX check box of one graph (plot1) is pressed, the logX check box of the other graph (plot2) is also checked and linked.

self, plotter1, self.plotter2 are of type PlotItem. If you created a graph with pg.PlotWidget, you can get it with getPlotItem ().

class 'pyqtgraph.graphicsItems.PlotItem.PlotItem.PlotItem'

For PlotWidget plotter = pg.PlotWidget() plotitem = plotter1.getPlotItem()

application

Change the graph display from the GUI button <img width=500 alt0'gui.gif' src='https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/779817/c9ec59cc-fd01-a073-61a5-70697e6741dc.gif'>

"""Change the graph display from the GUI button"""

import dataclasses
from typing import Optional
import sys

import numpy as np
from PyQt5 import QtWidgets
import pyqtgraph as pg

SAMPLE_DATA1 = np.linspace(-10, 10) ** 2
SAMPLE_DATA2 = 2 * np.linspace(-10, 10) ** 2


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

    def __post_init__(self) -> None:
        """Superclass loading and widget creation"""
        super(PlotLinkWindow, self).__init__(parent=self.parent)
        self.setGeometry(100, 100, 800, 500)
        self.setWindowTitle('PlotLinkWindow')

        self.create_widgets()
        self.create_layouts()
        self.set_layouts()
        self.connect_slot()

    def create_widgets(self) -> None:
        """Widget creation"""
        # toggle buttons
        self.fft_btn = QtWidgets.QPushButton('FFT')
        self.fft_btn.setCheckable(True)
        self.logX_btn = QtWidgets.QPushButton('logX')
        self.logX_btn.setCheckable(True)
        self.logY_btn = QtWidgets.QPushButton('logY')
        self.logY_btn.setCheckable(True)
        self.derivative_btn = QtWidgets.QPushButton('dy/dx')
        self.derivative_btn.setCheckable(True)
        self.phasemap_btn = QtWidgets.QPushButton("Y' vs Y")
        self.phasemap_btn.setCheckable(True)

        # graph
        self.plotter1 = pg.PlotWidget()
        self.plotter1.showGrid(x=True, y=True, alpha=0.8)
        self.plotter1_curve1 = self.plotter1.plot(pen=pg.mkPen('#f00', width=5))
        self.plotter1_curve2 = self.plotter1.plot(pen=pg.mkPen('#0f0', width=5))
        self.plotter1_curve1.setData(SAMPLE_DATA1)
        self.plotter1_curve2.setData(SAMPLE_DATA2)

        self.plotitem = self.plotter1.getPlotItem()

    def create_layouts(self) -> None:
        """Layout creation"""
        self.main_layout = QtWidgets.QHBoxLayout()
        self.button_layout = QtWidgets.QVBoxLayout()

    def set_layouts(self) -> None:
        """Set the widget in the layout"""
        self.main_layout.addLayout(self.button_layout)
        self.main_layout.addWidget(self.plotter1)

        self.button_layout.addWidget(self.fft_btn)
        self.button_layout.addWidget(self.logX_btn)
        self.button_layout.addWidget(self.logY_btn)
        self.button_layout.addWidget(self.derivative_btn)
        self.button_layout.addWidget(self.phasemap_btn)

        self.setLayout(self.main_layout)

    def connect_slot(self) -> None:
        """Slot connection"""
        self.fft_btn.clicked.connect(lambda: self.plotitem.ctrl.fftCheck.setChecked(self.fft_btn.isChecked()))
        self.logX_btn.clicked.connect(lambda: self.plotitem.ctrl.logXCheck.setChecked(self.logX_btn.isChecked()))
        self.logY_btn.clicked.connect(lambda: self.plotitem.ctrl.logYCheck.setChecked(self.logY_btn.isChecked()))
        self.derivative_btn.clicked.connect(
            lambda: self.plotitem.ctrl.derivativeCheck.setChecked(self.derivative_btn.isChecked()))
        self.phasemap_btn.clicked.connect(
            lambda: self.plotitem.ctrl.phasemapCheck.setChecked(self.phasemap_btn.isChecked()))


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


if __name__ == "__main__":
    main()

reference

Pyqtgraph, difference in track log/linear axis conversion between link axes pyqtgraph.graphicsItems.PlotItem.PlotItem — pyqtgraph 0.11.1.dev0 documentation Around here

self.ctrl = c = Ui_Form() ... c.fftCheck.toggled.connect(self.updateSpectrumMode) c.logXCheck.toggled.connect(self.updateLogMode) c.logYCheck.toggled.connect(self.updateLogMode) c.derivativeCheck.toggled.connect(self.updateDerivativeMode) c.phasemapCheck.toggled.connect(self.updatePhasemapMode)

itertools Wow, itertools-kun --Qiita

Recommended Posts

[pyqtgraph] Link Plot Options-> Transforms display
Enlarge the plot display.
3D scatter plot with PyQtGraph