Python/Matplotlib/Pyside 快速时间追踪滚动

标签 python animation matplotlib pyside

我有大量的时间痕迹,必须进行目视检查,因此我需要一个快速滚动工具。

如何实现最快的 Maplotlib/Pyside 滚动?

没错,我向 MPL 图添加了一个 PySide 滚动条,并使用 set_xlim() 方法更新了绘图的 x 范围。这还不够快,特别是因为在最终的应用程序中,我在不同的子图中至少有 8 个时间轨迹,它们必须全部滚动在一起。 A figure of the plot is attached .

还有改进的空间吗?

这里我附上演示代码,演示相对较低的滚动。它很长,但几乎都是样板代码。有趣的一点(需要改进)是在 xpos_changed() 方法中,其中绘图 xlimit 发生了更改。

编辑:下面我加入了tcaswell建议的一些微优化,但更新速度并没有提高。

from PySide import QtGui, QtCore
import pylab as plt
import numpy as np

N_SAMPLES = 1e6

def test_plot():
    time = np.arange(N_SAMPLES)*1e-3
    sample = np.random.randn(N_SAMPLES)
    plt.plot(time, sample, label="Gaussian noise")
    plt.title("1000s Timetrace \n (use the slider to scroll and the spin-box to set the width)")
    plt.xlabel('Time (s)')
    plt.legend(fancybox=True)
    q = ScrollingToolQT(plt.gcf(), scroll_step=10)
    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, scroll_step=10):
        # Setup data range variables for scrolling
        self.fig = fig
        self.scroll_step = scroll_step
        self.xmin, self.xmax = fig.axes[0].get_xlim()
        self.width = 1 # axis units
        self.pos = 0   # axis units
        self.scale = 1e3 # conversion betweeen scrolling units and axis units

        # Save some MPL shortcuts
        self.ax = self.fig.axes[0]
        self.draw = self.fig.canvas.draw
        #self.draw_idle = self.fig.canvas.draw_idle

        # Retrive the QMainWindow used by current figure and add a toolbar
        # to host the new widgets
        QMainWin = fig.canvas.parent()
        toolbar = QtGui.QToolBar(QMainWin)
        QMainWin.addToolBar(QtCore.Qt.BottomToolBarArea, toolbar)

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

        # Set the initial xlimits coherently with values in slider and spinbox
        self.ax.set_xlim(self.pos,self.pos+self.width)
        self.draw()

    def set_slider(self, parent):
        self.slider = QtGui.QSlider(QtCore.Qt.Horizontal, parent=parent)
        self.slider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.slider.setTickInterval((self.xmax-self.xmin)/10.*self.scale)
        self.slider.setMinimum(self.xmin*self.scale)
        self.slider.setMaximum((self.xmax-self.width)*self.scale)
        self.slider.setSingleStep(self.width*self.scale/4.)
        self.slider.setPageStep(self.scroll_step*self.width*self.scale)
        self.slider.setValue(self.pos*self.scale) # 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.width)   # 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)
        pos /= self.scale
        self.ax.set_xlim(pos, pos+self.width)
        self.draw()

    def xwidth_changed(self, width):
        #pprint("Width (axis units) %f\n" % step)
        if width <= 0: return
        self.width = width
        self.slider.setSingleStep(self.width*self.scale/5.)
        self.slider.setPageStep(self.scroll_step*self.width*self.scale)
        old_xlim = self.ax.get_xlim()
        self.xpos_changed(old_xlim[0]*self.scale)


if __name__ == "__main__":
    q = test_plot()
    plt.show()

最佳答案

根据评论中的要求,这里是一个 pyqtgraph 演示,它将两条大迹线滚动在一起(通过鼠标)。

pyqtgraph 项目的文档并不完整,但您可以使用 python -m pyqtgraph.examples 查看一些很好的示例,它们应该会为您指明正确的方向。 crosshair.py 示例可能对您特别感兴趣。

如果您使用 pyqtgraph,请将 slider 小部件连接到本演示最后一行中的 setXRange 方法。

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
app = QtGui.QApplication([])
win = pg.GraphicsWindow()

x = np.arange(1e5)
y1 = np.random.randn(x.size)
y2 = np.random.randn(x.size)
p1 = win.addPlot(x=x, y=y1, name='linkToMe')
p1.setMouseEnabled(x=True, y=False)
win.nextRow()
p2 = win.addPlot(x=x, y=y2)
p2.setXLink('linkToMe')
p1.setXRange(2000,3000)

enter image description here

关于Python/Matplotlib/Pyside 快速时间追踪滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16824718/

相关文章:

python - 从 Fortran/C 调用 Python 函数

python - 在Python中使用后释放缓冲区空间?

python - 计算 pandas 中不连续的行索引集的列值差异

vb.net - 是否可以使用标准控件生成动画过渡?

javascript - FabricJS 奇怪的线条动画与 pageSnap

java - 在带有动画的 Java 应用程序中使用多个计时器

python - python 2.7 的 Pandas 导入错误

matplotlib - 如何创建开始和结束在特定范围内的条形图

Matplotlib 轮廓图上颜色条的 Python 最小和最大范围

带有日期时间基础的python plot stem