質問

Assume that I have two views\plots created in pyqtgraph and then they are linked. using lines

p2.setYLink('Plot1')
p2.setXLink('Plot1')

Question is that when we link the views, the ranges of both the views are made equal, which raises issue as one plot appears to be too much zoomed out or zoomed in. We just want to link the views to zoom together but don't want the ranges to change as the plot looks changed.

Below is sample code that explains the issue visually

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

#QtGui.QApplication.setGraphicsSystem('raster')
try:
    app = QtGui.QApplication(sys.argv)
except RuntimeError:
    app = QtCore.QCoreApplication.instance()

x1 = [1,2,3,4,5]
y1 = x1

x2 = [10,20,30,40,50]
y2 = x2

win = pg.GraphicsWindow(title="pyqtgraph example: Linked Views")
win.resize(800,600)

win.addLabel("Linked Views", colspan=2)
win.nextRow()

p1 = win.addPlot(x=x1, y=y1, name="Plot1", title="Plot1")
p2 = win.addPlot(x=x2, y=y2, name="Plot2", title="Plot2: Y linked with Plot1")
p2_state = p2.vb.getState()
p1_state = p1.vb.getState()

p2.setLabel('bottom', "Label to test offset")
p2.setYLink('Plot1')  ## test linking by name
p2.setXLink('Plot1')

app.exec_()
役に立ちましたか?

解決

To restate the question: you want to have two views that can have different ranges and scales, but when you zoom with the mouse in one view, the other view will zoom by the same amount.

This is not the intended function of the setXLink/setYLink methods, so you will need to implement this by subclassing or monkey-patching the viewboxes. For example:

import pyqtgraph as pg

p1 = pg.plot([1,6,2,4,3])
p2 = pg.plot([30,50,10,70,20])

def scaleBy(*args, **kwds):
    pg.ViewBox.scaleBy(p1.plotItem.vb, *args, **kwds)
    pg.ViewBox.scaleBy(p2.plotItem.vb, *args, **kwds)

p1.plotItem.vb.scaleBy = scaleBy
p2.plotItem.vb.scaleBy = scaleBy

There is a problem, however, that you need know two things when scaling: how much to scale by (this is the same for both views, so not a problem), and around what point to scale (this is different between the views, so a bit more difficult to determine). The solution to this depends on your desired behavior.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top