Is it possible to zoom the plot in real time ?

172 views
Skip to first unread message

actis oh

unread,
Jun 19, 2016, 11:28:45 PM6/19/16
to pyqtgraph
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 pg
from pyqtgraph.Qt import QtCore, QtGui
import 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 plots
def 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_()

actis oh

unread,
Jun 20, 2016, 3:46:25 AM6/20/16
to pyqtgraph
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()

actis oh

unread,
Jun 21, 2016, 2:06:13 AM6/21/16
to pyqtgraph
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]) 

2016년 6월 20일 월요일 오후 4시 46분 25초 UTC+9, actis oh 님의 말:
Reply all
Reply to author
Forward
0 new messages