> To avoid infinite loops, you need to make sure that in #2, updating the
> region item does not cause plot1 X range to be updated again. Also, since
> you are automatically setting the Y range for all plots, you should disable
> mouse interaction for that axis with plot.setMouseEnabled(y=False).
I can't get this to work. I've spent a significant time on this and
can't seem to find what the problem is.
For #2 -> I disable all update and and update region event handlers, I
set the x range and the region and then enable the event handlers
again. The region window works fine, and dragging in plot 3 works
fine. However if you drag x on plot 1 or plot 2 things go bad. (see
code below)
In the code below, I need to set an updateregion for each plot window
since I need to know which plot window I'm in to calculate the y
range.
---------------------------------------------------------------
import initExample ## Add path to library (just for examples; you do
not need this)
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point
from random import random
#genearte layout
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
p1 = win.addPlot(row=1, col=0)
p2 = win.addPlot(row=2, col=0)
p3 = win.addPlot(row=3, col=0)
p4 = win.addPlot(row=4, col=0)
region = pg.LinearRegionItem()
region.setZValue(10)
p4.addItem(region)
#create numpy arrays
#make the numbers large to show that the xrange shows data from 10000
to all the way 0
data1 = 10000 + 3000 * np.random.random(size=10000)
data2 = 15000 + 3000 * np.random.random(size=10000)
p1.plot(data1, pen="r")
p2.plot(data2, pen="g")
p3.plot(data1, pen="w")
p4.plot(data2, pen="w")
for window in [p1, p2, p3]:
window.setMouseEnabled(y=False)
def update():
for i, window in enumerate([p1, p2, p3]):
try:
if (i, "updateRegion") in functionCache:
window.sigRangeChanged.disconnect(functionCache[(i,
"updateRegion")])
except:
print 'Error: '
for window in [p1, p2, p3]:
window.setXRange(*region.getRegion())
for i, window in enumerate([p1, p2, p3]):
try:
if (i, "updateRegion") in functionCache:
window.sigRangeChanged.connect(functionCache[(i,
"updateRegion")])
except:
print 'Error: '
region.sigRegionChanged.connect(update)
region.setRegion([1000, 2000])
functionCache = {}
for i, window in enumerate([p1, p2, p3]):
def updateRegion(view, viewRange):
region.sigRegionChanged.disconnect(update)
for j, window in enumerate([p1, p2, p3]):
try:
if (j, "updateRegion") in functionCache:
window.sigRangeChanged.disconnect(functionCache[(j, "updateRegion")])
except:
print 'Error: '
minX, maxX = viewRange[0]
region.setRegion(viewRange[0])
for j, window in enumerate([p1, p2, p3]):
window.setYRange(10000 - 5000*random(),
10000+5000*random())
if i != j:
window.setXRange(minX, maxX)
for j, window in enumerate([p1, p2, p3]):
try:
if (j, "updateRegion") in functionCache:
window.sigRangeChanged.connect(functionCache[(j,
"updateRegion")])
except:
print 'Error: '
region.sigRegionChanged.connect(update)
functionCache[(i, "updateRegion")] = updateRegion
window.sigRangeChanged.connect(updateRegion)
## Start Qt event loop unless running in interactive mode or using
pyside.
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore,
'PYQT_VERSION'):
app.exec_()