I'm attempting to plot the same line on two plots in pyqtgraph (pyqt5). Ideally, any changes to the line are reflected in both plots. For example, if the line is moved (mouse dragged) on one plot, the line on the other plot should move as well.
I am currently achieving this by making two instances of the line, and adding one to each plot. This forces me to connect the lines together via signals to ensure that any property changes are reflected in both (moving the line, deleting the line, etc).
This works okay for now, if a bit clunky, but I may have hundreds or thousands of lines to plot, so I'm wondering if there is a better way to plot the same item on two plots without making a copy of each item, thus doubling the total number of items needed.
Here is a minimal example of my current method:
import sys
from PyQt5.QtWidgets import QApplication
from pyqtgraph import GraphicsLayoutWidget, PlotItem, InfiniteLine
class Line(InfiniteLine):
"""Infinite Line with a method to set position from another Infinite Line's position."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def setPosition(self, line):
self.blockSignals(True)
self.setPos(line.pos())
self.blockSignals(False)
# app
app = QApplication(sys.argv)
# lines
line1 = Line(5, movable = True)
line2 = Line(5, movable = True)
line1.sigPositionChanged.connect(line2.setPosition)
line2.sigPositionChanged.connect(line1.setPosition)
# plots
plot1 = PlotItem()
plot2 = PlotItem()
plot1.setXRange(0, 10)
plot2.setXRange(0, 10)
plot1.addItem(line1)
plot2.addItem(line2)
# widget
widget = GraphicsLayoutWidget()
widget.addItem(plot1, 0, 0)
widget.addItem(plot2, 1, 0)
widget.show()
# run
app.exec()