PyQtGraph:当setClipToView设置为True时,不会绘制出最后一个元素

时间:2015-04-24 07:44:05

标签: python pyqtgraph

为了减少绘制gui更新的时间,我设置了setClipToView(True),以节省绘制图表中不必要(看不见的)部分的CPU时间。我的代码如下。

import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
from PySide.QtCore import QTime
import sys
import numpy as np
import time
from collections import deque

app = QtGui.QApplication([])
grams = deque()
penColor = pg.mkPen(color=(0,0,0),width=2)

pg.setConfigOption('background','w')
pg.setConfigOption('foreground','k')
pg.setConfigOptions(antialias=True)
view = pg.GraphicsView()
outerLayout = pg.GraphicsLayout()
outerLayout.layout.setSpacing(0)
view.setCentralItem(outerLayout)
view.show()
l2 = outerLayout.addLayout()

lowerGraph = l2.addPlot(name = "lower")
lowerGraph.showGrid(x=True,y=True)
lowerGraph.setClipToView(True)
lowerCurve = lowerGraph.plot(pen = penColor)

def update(grams):
    x = [item['x'] for item in grams]
    yLower = [item['y'] for item in grams]
    lowerGraph.setXRange(0,x[-1]+20)
    lowerCurve.setData(x=x, y=yLower)
    QtGui.QApplication.processEvents()
i = 0
j = 0.0
while i < 4:
    gram = np.random.uniform(0,51,size=1)
    grams.append({'x':j,'y':gram[0]})
    update(grams)
    i += 1
    j += 1000
    time.sleep(0.1)

lowerCurve.clear()
x = [item['x'] for item in grams]
y = [item['y'] for item in grams]
lowerGraph.setXRange(0,x[-1]+20)
lowerCurve = lowerGraph.plot(x=x,y=y,pen = penColor)

QtGui.QApplication.instance().exec_()

当我注释掉setToCliptoView(True)时,数据的最后一个元素也被绘制出来,但是其他方式会从绘图中丢弃它。

我猜这是一个错误。 感谢您的时间。 问候, UPOL

1 个答案:

答案 0 :(得分:0)

这是PlotDataItem的getData函数中的一个错误,它会在您动态更新剪切数据时显示:https://groups.google.com/forum/#!topic/pyqtgraph/p1UE-zvSI2g

在最新的开发分支中,从

更改PlotDataItem.py行564
x1 = np.clip(int((range.right()-x[0])/dx)+2*ds , 0, len(x)-1)

x1 = np.clip(int((range.right()-x[0])/dx)+2*ds , 0, len(x))