Python 用vispy设置图像堆栈动画

Python 用vispy设置图像堆栈动画,python,matlab,matplotlib,vispy,glumpy,Python,Matlab,Matplotlib,Vispy,Glumpy,我正在尝试从MATLAB迁移到Python,在MATLAB的开发过程中,我经常依赖的一个功能是通过循环层和调用drawnow快速可视化数据立方体的切片 tst = randn(1000,1000,100); for n = 1:size(tst, 3) imagesc(tst(:,:,n)); drawnow; end 当我在MATLAB中输入tic/toc时,它显示该图以大约28fps的速度更新。相反,当我尝试使用matplotlib的imshow()命令执行此操作时,与之相

我正在尝试从MATLAB迁移到Python,在MATLAB的开发过程中,我经常依赖的一个功能是通过循环层和调用drawnow快速可视化数据立方体的切片

tst = randn(1000,1000,100);
for n = 1:size(tst, 3)
    imagesc(tst(:,:,n));
    drawnow;
end
当我在MATLAB中输入tic/toc时,它显示该图以大约28fps的速度更新。相反,当我尝试使用matplotlib的imshow()命令执行此操作时,与之相比,它的运行速度非常慢,甚至使用set_data()

在我的电脑上,默认(非常小)的速度是16fps左右,如果我把它调整到更大,与matlab图形大小相同,速度会降低到5fps左右。从一些较旧的线程中,我看到了使用glumpy的建议,我将其与所有适当的包和库(glfw等)一起安装,包本身工作正常,但它不再支持以前简单的图像可视化

然后我下载了vispy,我可以用它制作一个图像,使用中的代码作为模板:

    import sys
    from vispy import scene
    from vispy import app
    import numpy as np

    canvas = scene.SceneCanvas(keys='interactive')
    canvas.size = 800, 600
    canvas.show()

    # Set up a viewbox to display the image with interactive pan/zoom
    view = canvas.central_widget.add_view()

    # Create the image
    img_data = np.random.random((800,800, 3))
    image = scene.visuals.Image(img_data, parent=view.scene)
    view.camera.set_range()

    # unsuccessfully tacked on the end to see if I can modify the figure.
    # Does nothing.
    img_data_new = np.zeros((800,800, 3))
    image = scene.visuals.Image(img_data_new, parent=view.scene)
    view.camera.set_range()
Vispy看起来非常快,这看起来会让我达到目的,但是如何用新数据更新画布呢?谢谢,

请参阅方法


显示了一个在matplotlib中加快此速度的解决方案,也是其他python库(PyQtGraph)的替代方案。我将研究PyQtGraph,它可能就是我要寻找的。感谢您的回复。这对我不起作用,数字不会改变&不会抛出错误。没关系,它起作用了!非常感谢。我在以前测试它时意外地复制了一行代码,这就是为什么它不工作的原因。然而,如果我把它放在一个循环中,尝试设置一些动画,它不会不断更新。
    import sys
    from vispy import scene
    from vispy import app
    import numpy as np

    canvas = scene.SceneCanvas(keys='interactive')
    canvas.size = 800, 600
    canvas.show()

    # Set up a viewbox to display the image with interactive pan/zoom
    view = canvas.central_widget.add_view()

    # Create the image
    img_data = np.random.random((800,800, 3))
    image = scene.visuals.Image(img_data, parent=view.scene)
    view.camera.set_range()

    # unsuccessfully tacked on the end to see if I can modify the figure.
    # Does nothing.
    img_data_new = np.zeros((800,800, 3))
    image = scene.visuals.Image(img_data_new, parent=view.scene)
    view.camera.set_range()
# Create the image
img_data = np.random.random((800,800, 3))
image = scene.visuals.Image(img_data, parent=view.scene)
view.camera.set_range()

# Generate new data :
img_data_new = np.zeros((800,800, 3))
img_data_new[400:, 400:, 0] = 1.  # red square
image.set_data(img_data_new)