我正在尝试从MATLAB迁移到Python,在Matlab开发期间,我经常依赖的一件事是通过循环遍历各层并调用drawnow来快速可视化datacube切片的能力。
tst = randn(1000,1000,100);
for n = 1:size(tst, 3)
imagesc(tst(:,:,n));
drawnow;
end当I tic/toc this in MATLAB时,它显示图形正在以大约28fps的速度更新。相比之下,当我尝试使用matplotlib的imshow()命令执行此操作时,即使使用set_data(),运行速度也非常缓慢。
import matplotlib as mp
import matplotlib.pyplot as plt
import numpy as np
tmp = np.random.random((1000,1000,100))
myfig = plt.imshow(tmp[:,:,i], aspect='auto')
for i in np.arange(0,tmp.shape[2]):
myfig.set_data(tmp[:,:,i])
mp.pyplot.title(str(i))
mp.pyplot.pause(0.001)在我的计算机上,默认(非常小)的比例大约是16fps,如果我把它的大小调整到与matlab的数字相同的大小,它会减慢到大约5fps。在一些较老的线程中,我看到了使用glumpy的建议,我将其与所有适当的包和库(glfw等)一起安装,包本身运行良好,但它不再支持suggested in a previous thread的简单图像可视化。
然后我下载了vispy,我可以使用this thread中的代码作为模板来用它制作图像:
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似乎非常快,这看起来会让我达到目标,但是如何使用新数据更新画布呢?谢谢,
https://stackoverflow.com/questions/51218957
复制相似问题