首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyqtgraph/chaco/guiqwt:快速滚动时间表竞赛演示

pyqtgraph/chaco/guiqwt:快速滚动时间表竞赛演示
EN

Stack Overflow用户
提问于 2013-05-31 03:48:43
回答 1查看 2.9K关注 0票数 2

我想用python实现一个快速滚动的时间表竞赛工具。timetrace数据已经在内存中的numpy数组中,并且很大(>1e6个样本)。我需要一个快速目视检查的工具。

我使用Matplotlib+PySide进行already tried,但是更新速度不够快。

你能在另一个工具包中重现Matplotlib+Pyside demo吗?我不认识他们中的任何一个,我愿意学习在这个应用程序中表现更好的。

为了在我的工作流程中有用,选择的框架应该允许从交互式ipython会话运行绘图,并且应该是快速和可扩展的(最终,我需要在同一窗口上同步滚动几个绘图)。原则上,pyqtgraph、guiqwt或chaco似乎都是很好的候选者。但让我们根据一个真实的例子来判断。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-31 06:54:08

这是pyqtgraph版本。我尽量保持代码与原始演示的相似。在我的系统上,pyqtgraph的运行速度只比matplotlib快5倍,而且在所有数据都可见的情况下仍然相当慢(~1fps)。matplotlib和pyqtgraph之间的主要性能差异在于吞吐量--绘制新数据的速度有多快。

为了获得更好的性能,我推荐使用一些基于GPU的绘图库,比如visvis或galry。Pyqtgraph将在未来添加GPU支持,但它还没有实现。也有一些将matplotlib引入GPU的努力,但我还没有看到任何结果。

代码语言:javascript
复制
## adapted from http://stackoverflow.com/questions/16824718/python-matplotlib-pyside-fast-timetrace-scrolling

from PySide import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

N_SAMPLES = 1e6

def test_plot():
    time = np.arange(N_SAMPLES)*1e-3
    sample = np.random.randn(N_SAMPLES)

    plt = pg.PlotWidget(title="Use the slider to scroll and the spin-box to set the width")
    plt.addLegend()
    plt.plot(time, sample, name="Gaussian noise")
    q = ScrollingToolQT(plt)
    return q   # WARNING: it's important to return this object otherwise
            # python will delete the reference and the GUI will not respond!


class ScrollingToolQT(object):
    def __init__(self, fig):
        # Setup data range variables for scrolling
        self.fig = fig
        self.xmin, self.xmax = fig.plotItem.vb.childrenBounds()[0]
        self.step = 1 # axis units

        self.scale = 1e3 # conversion betweeen scrolling units and axis units

        # Retrive the QMainWindow used by current figure and add a toolbar
        # to host the new widgets
        self.win = QtGui.QMainWindow()
        self.win.show()
        self.win.resize(800,600)
        self.win.setCentralWidget(fig)
        self.toolbar = QtGui.QToolBar()
        self.win.addToolBar(QtCore.Qt.BottomToolBarArea, self.toolbar)

        # Create the slider and spinbox for x-axis scrolling in toolbar
        self.set_slider(self.toolbar)
        self.set_spinbox(self.toolbar)

        # Set the initial xlimits coherently with values in slider and spinbox
        self.set_xlim = self.fig.setXRange
        self.set_xlim(0, self.step)

    def set_slider(self, parent):
        # Slider only support integer ranges so use ms as base unit
        smin, smax = self.xmin*self.scale, self.xmax*self.scale

        self.slider = QtGui.QSlider(QtCore.Qt.Horizontal, parent=parent)
        self.slider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.slider.setTickInterval((smax-smin)/10.)
        self.slider.setMinimum(smin)
        self.slider.setMaximum(smax-self.step*self.scale)
        self.slider.setSingleStep(self.step*self.scale/5.)
        self.slider.setPageStep(self.step*self.scale)
        self.slider.setValue(0)  # set the initial position
        self.slider.valueChanged.connect(self.xpos_changed)
        parent.addWidget(self.slider)

    def set_spinbox(self, parent):
        self.spinb = QtGui.QDoubleSpinBox(parent=parent)
        self.spinb.setDecimals(3)
        self.spinb.setRange(0.001, 3600.)
        self.spinb.setSuffix(" s")
        self.spinb.setValue(self.step)   # set the initial width
        self.spinb.valueChanged.connect(self.xwidth_changed)
        parent.addWidget(self.spinb)

    def xpos_changed(self, pos):
        #pprint("Position (in scroll units) %f\n" %pos)
        #        self.pos = pos/self.scale
        pos /= self.scale
        self.set_xlim(pos, pos + self.step, padding=0)

    def xwidth_changed(self, xwidth):
        #pprint("Width (axis units) %f\n" % step)
        if xwidth <= 0: return
        self.step = xwidth
        self.slider.setSingleStep(self.step*self.scale/5.)
        self.slider.setPageStep(self.step*self.scale)
        old_xlim = self.fig.plotItem.vb.viewRange()[0]
        self.xpos_changed(old_xlim[0] * self.scale)

if __name__ == "__main__":
    app = pg.mkQApp()
    q = test_plot()
    app.exec_()
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16844693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档