Discussion:
[pyqtgraph] Is it possible to zoom the plot in real time ?
actis oh
2016-06-20 03:28:45 UTC
Permalink
I suffer from zooming the plot in real time.
This plot draw random function in real time.
Problem is not shown right the 2nd plot zoomed.
It must be something I have done wrong.
Or is it possible to zoom the plot in real time?
I will appreciate to help me.
Used example code is "plotting.py".


# -*- coding: utf-8 -*-
import pyqtgraph as pgfrom pyqtgraph.Qt import QtCore, QtGuiimport numpy as np

win = pg.GraphicsWindow()
win.setWindowTitle('Scroll and Zoomed Plot')

plotScroll = win.addPlot()
plotScroll.setDownsampling(mode='peak')
plotScroll.setClipToView(True)
curveScroll = plotScroll.plot()

dataRnd = np.empty(100)
ptrDataRnd = 0
def updateScroll():
global dataRnd, ptrDataRnd
dataRnd[ptrDataRnd] = np.random.normal()
ptrDataRnd += 1
if ptrDataRnd >= dataRnd.shape[0]:
tmp = dataRnd
dataRnd = np.empty(dataRnd.shape[0] * 2)
dataRnd[:tmp.shape[0]] = tmp
curveScroll.setData(dataRnd[:ptrDataRnd])
LinRegionItem = pg.LinearRegionItem([0,100])LinRegionItem.setZValue(-10)
plotScroll.addItem(LinRegionItem)

win.nextRow()

plotZoom = win.addPlot(title="Zoomed graph for Random plot ")
plotZoom.plot(dataRnd, pen=(255,255,255,200))
def updatePlot():
plotZoom.setXRange(*LinRegionItem.getRegion(), padding=0)def updateRegion():
LinRegionItem.setRegion(plotZoom.getViewBox().viewRange()[0])LinRegionItem.sigRegionChanged.connect(updatePlot)
plotZoom.sigXRangeChanged.connect(updateRegion)
updatePlot()
# update all plotsdef update():
updateScroll()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)
## Start Qt event loop unless running in interactive mode or using pyside.if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/387d5df2-d712-4d4d-a990-3086da14a7a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
actis oh
2016-06-20 07:46:24 UTC
Permalink
I solved this problem with adding "updateZoom()" function.
Code revised is shown as below.
And another problem occurred as the console message below.
What is the reason?
Would you please help me?

Console message :

*C:\Python27\lib\site-packages\pyqtgraph\graphicsItems\AxisItem.py:841:
RuntimeWarning: overflow encountered in double_scalars*
* xScale = -bounds.height() / dif*
*C:\Python27\lib\site-packages\pyqtgraph\graphicsItems\AxisItem.py:847:
RuntimeWarning: invalid value encountered in double_scalars*
* xRange = [x * xScale - offset for x in self.range]*
*C:\Python27\lib\site-packages\pyqtgraph\graphicsItems\AxisItem.py:871:
RuntimeWarning: invalid value encountered in double_scalars*
* x = (v * xScale) - offset*

*Process finished with exit code 0*

Revised code :

# -*- coding: utf-8 -*-

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

app = QtGui.QApplication([])
gui = QtGui.QMainWindow()
area = DockArea()
gui.setCentralWidget(area)
gui.resize(1000,500)
gui.setWindowTitle("Scroll and Zoomed Plot")

dockCtrl = Dock("Plot Control", size=(200, 500))
dockCtrl.setFixedWidth(200)
area.addDock(dockCtrl, 'left')
winCtrl = pg.LayoutWidget()
dockCtrl.addWidget(winCtrl)

restartBttn = QtGui.QPushButton("Restart")
restartBttn.setMinimumSize(100,40)
winCtrl.addWidget(restartBttn)

def RestartPlot():
global dataRnd,ptrDataRnd
timer.stop()
dataRnd = np.empty(100)
ptrDataRnd = 0
timer.start(50)

restartBttn.clicked.connect(RestartPlot)

dockScroll = Dock("Scrolling plot", size=(800,250))
area.addDock(dockScroll, 'right')
winScroll = pg.GraphicsWindow()
dockScroll.addWidget(winScroll)

dockZoom = Dock("Zoomed plot", size=(800,250))
area.addDock(dockZoom, 'right')
winZoom = pg.GraphicsWindow()
dockZoom.addWidget(winZoom)
area.moveDock(dockScroll, 'top', dockZoom)

plotScroll = winScroll.addPlot()
plotScroll.setDownsampling(mode='peak')
plotScroll.setClipToView(True)
curveScroll = plotScroll.plot()

dataRnd = np.empty(100)
ptrDataRnd = 0

def updateScroll():
global dataRnd, ptrDataRnd
dataRnd[ptrDataRnd] = np.random.normal()
ptrDataRnd += 1
if ptrDataRnd >= dataRnd.shape[0]:
tmp = dataRnd
dataRnd = np.empty(dataRnd.shape[0] * 2)
dataRnd[:tmp.shape[0]] = tmp
curveScroll.setData(dataRnd[:ptrDataRnd])

LinRegionItem = pg.LinearRegionItem([0,100])
LinRegionItem.setZValue(-10)
plotScroll.addItem(LinRegionItem)

plotZoom = winZoom.addPlot()
curveZoom = plotZoom.plot(dataRnd, pen=(255,255,255,200))

def updatePlot():
plotZoom.setXRange(*LinRegionItem.getRegion(), padding=0)
def updateRegion():
LinRegionItem.setRegion(plotZoom.getViewBox().viewRange()[0])

LinRegionItem.sigRegionChanged.connect(updatePlot)
plotZoom.sigXRangeChanged.connect(updateRegion)
updatePlot()

# added lines
def updateZoom():
curveZoom.setData(dataRnd[:ptrDataRnd])

# update all plots
def update():
updateScroll()
# added lines

updateZoom()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)

gui.show()

if __name__ == '__main__':
import sys

if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/77cd6e55-1089-4a43-9524-bc49f03b88a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
actis oh
2016-06-21 06:06:13 UTC
Permalink
I also solved this problem. I revised the code as shown below line.
"curveZoom.setData(dataRnd[:ptrDataRnd])" ->
"curveZoom.setData(dataRnd[0:ptrDataRnd])"
I wonder the convention of the numpy array.
The meaning of "dataRnd[:ptrDataRnd]" is to specify range from 0th to
ptrDataRnd. Am I wrong?
Please tell me the truth.

# added lines
def updateZoom():

curveZoom.setData(dataRnd[0:ptrDataRnd])
Post by actis oh
I solved this problem with adding "updateZoom()" function.
Code revised is shown as below.
And another problem occurred as the console message below.
What is the reason?
Would you please help me?
RuntimeWarning: overflow encountered in double_scalars*
* xScale = -bounds.height() / dif*
RuntimeWarning: invalid value encountered in double_scalars*
* xRange = [x * xScale - offset for x in self.range]*
RuntimeWarning: invalid value encountered in double_scalars*
* x = (v * xScale) - offset*
*Process finished with exit code 0*
# -*- coding: utf-8 -*-
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
from pyqtgraph.dockarea import *
app = QtGui.QApplication([])
gui = QtGui.QMainWindow()
area = DockArea()
gui.setCentralWidget(area)
gui.resize(1000,500)
gui.setWindowTitle("Scroll and Zoomed Plot")
dockCtrl = Dock("Plot Control", size=(200, 500))
dockCtrl.setFixedWidth(200)
area.addDock(dockCtrl, 'left')
winCtrl = pg.LayoutWidget()
dockCtrl.addWidget(winCtrl)
restartBttn = QtGui.QPushButton("Restart")
restartBttn.setMinimumSize(100,40)
winCtrl.addWidget(restartBttn)
global dataRnd,ptrDataRnd
timer.stop()
dataRnd = np.empty(100)
ptrDataRnd = 0
timer.start(50)
restartBttn.clicked.connect(RestartPlot)
dockScroll = Dock("Scrolling plot", size=(800,250))
area.addDock(dockScroll, 'right')
winScroll = pg.GraphicsWindow()
dockScroll.addWidget(winScroll)
dockZoom = Dock("Zoomed plot", size=(800,250))
area.addDock(dockZoom, 'right')
winZoom = pg.GraphicsWindow()
dockZoom.addWidget(winZoom)
area.moveDock(dockScroll, 'top', dockZoom)
plotScroll = winScroll.addPlot()
plotScroll.setDownsampling(mode='peak')
plotScroll.setClipToView(True)
curveScroll = plotScroll.plot()
dataRnd = np.empty(100)
ptrDataRnd = 0
global dataRnd, ptrDataRnd
dataRnd[ptrDataRnd] = np.random.normal()
ptrDataRnd += 1
tmp = dataRnd
dataRnd = np.empty(dataRnd.shape[0] * 2)
dataRnd[:tmp.shape[0]] = tmp
curveScroll.setData(dataRnd[:ptrDataRnd])
LinRegionItem = pg.LinearRegionItem([0,100])
LinRegionItem.setZValue(-10)
plotScroll.addItem(LinRegionItem)
plotZoom = winZoom.addPlot()
curveZoom = plotZoom.plot(dataRnd, pen=(255,255,255,200))
plotZoom.setXRange(*LinRegionItem.getRegion(), padding=0)
LinRegionItem.setRegion(plotZoom.getViewBox().viewRange()[0])
LinRegionItem.sigRegionChanged.connect(updatePlot)
plotZoom.sigXRangeChanged.connect(updateRegion)
updatePlot()
# added lines
curveZoom.setData(dataRnd[:ptrDataRnd])
# update all plots
updateScroll()
# added lines
updateZoom()
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)
gui.show()
import sys
QtGui.QApplication.instance().exec_()
--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/296d0ef2-4c56-44df-adb0-95e7d88f6b9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...