如何在pyqtgraph中缩放图

时间:2016-06-13 11:05:52

标签: python numpy pyqt pyqtgraph

我不知道如何操作要缩放的绘图 该图实时绘制随机函数 我想实时缩放指定区域的情节 此代码是pyqtgraph中的示例代码。此代码适用于固定数据 情节是否无法实时移动以进行缩放? 你能帮帮我吗?

# -*- 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_()

2 个答案:

答案 0 :(得分:0)

我通过添加标记为“#added ..”的“updateZoom()”函数解决了这个问题 修订的代码如下所示  并且下面的控制台消息发生了另一个问题 是什么原因?
你能帮帮我吗?

控制台消息:

C:\ Python27 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ AxisItem.py:841:RuntimeWarning:在double_scalars中遇到溢出   xScale = -bounds.height()/ dif
C:\ Python27 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ AxisItem.py:847:RuntimeWarning:double_scalars中遇到无效值   xRange = [x * xScale - 自我范围中x的偏移量]
C:\ Python27 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ AxisItem.py:871:RuntimeWarning:double_scalars中遇到无效值   x =(v * xScale) - 偏移量

处理完成,退出代码为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)

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 line
    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_()

答案 1 :(得分:0)

我也解决了这个问题。我修改了代码,如下图所示。

修订前:

curveZoom.setData(dataRnd[:ptrDataRnd])  
修改后的代码:

curveZoom.setData(dataRnd[0:ptrDataRnd])

我想知道numpy数组的约定。 “dataRnd [:ptrDataRnd]”的含义是指定从0th到ptrDataRnd的范围。我错了吗? 请说实话。