使用plt.imshow可获得更快的刷新率 [英] A faster refresh rate with plt.imshow

查看:405
本文介绍了使用plt.imshow可获得更快的刷新率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在进行numpy计算时显示一些图像:

I'd like to display some images while doing a numpy computation:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()  # Turn the interactive mode on.
for i in range(100):
    A = np.random.randn(10,10)
    plt.imshow(A)
    plt.pause(0.001)
    # do some other numpy computations here (they take < 1 ms)

速度不是很快显示图像,而是很慢.

我并不是要求每秒100帧,但我认为可以达到30 fps,但事实并非如此:经过几次迭代,我在标准i5笔记本电脑(Windows 7 x64)上接近2 fps.

I'm not asking for 100 frames per second, but I thought 30 fps would be possible, but it's not: after a few iterations, I'm close to 2 fps on my standard i5 laptop (Windows 7 x64).

如何提高imshow刷新速度?

How to have a faster imshow refresh rate?

注意:

  • I've already tried the main answer from Fast Live Plotting in Matplotlib / PyPlot, but here it seems a complex method (using blit parameter) for such a simple task and also I don't get 28 fps but only 15 fps.

我只想将矩阵显示为图像:没有边框,没有轴,没有子图等,我想这可以比解决方案

I only want to display the matrix as image: no border, no axes, no subplot, etc., I imagine this could be done faster than the solution Fast Live Plotting in Matplotlib / PyPlot, maybe not with matplotlib but another library?

推荐答案

这是因为您在每次迭代中都创建了一个新图像,最终在您的图形中生成了100张图像.

This is because you create a new image in each iteration, eventually leading to 100 images in your figure.

创建动画的推荐方法是使用FuncAnimation并仅更改图像的数据,而不是一直绘制新图像.

The recommended way to create an animation would be to use FuncAnimation and to only change the image's data, instead of plotting a new image all the time.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

im = plt.imshow(np.random.randn(10,10))

def update(i):
    A = np.random.randn(10,10)
    im.set_array(A)
    return im, text

ani = FuncAnimation(plt.gcf(), update, frames=range(100), interval=5, blit=False)

plt.show()

即使interval设置为5毫秒,以上代码在我的计算机上也以50 fps的速度运行.它不会比它能更快地进行.您现在可以使用blitting,即blit=True,在这种情况下,我看到的是100 fps.这是matplotlib可以达到的极限,但是当然会因计算机的性能而异.

Even though the interval is set to 5 ms, the above code runs with 50 fps on my computer. It will not go faster than it can. You may now use blitting, i.e. blit=True, in which case I see 100 fps. This is the limit of what matplotlib can achieve, but it will of course vary depending on the computer power.

但是请注意,人脑无法解析100 fps.有人说25是通常的帧速率,因此大多数电影也使用这种帧速率.因此,这里甚至不需要使用blitting,因为50 fps比您能感知的大.

Note however, that the human brain is not capable of resolving 100 fps. One says, 25 is the usual framerate, such that most movies use such framerate as well. So there shouldn't even be the need to use blitting here, as 50 fps is larger than what you are able to perceive.

如果出于任何原因想要更快地处理动画,则需要使用除matplotlib之外的其他库.

If for any reason you want to go faster with your animation, you need to use other libraries than matplotlib.

例如参见

  • Matplotlib, refresh image with imshow faster
  • Matplotlib equivalent of pygame flip

在已编辑的问题中,一个句子表示不应有边界.这是通过使图形尺寸服从图像的外观(正方形图像->正方形图形)并将所有边距设置为零来实现的

One sententence in the edited question says that there should be no border. This is accomplished by making the figure size obey the aspect of the image (square image -> square figure) and setting all margins to zero

plt.figure(figsize=(5,5))
plt.subplots_adjust(0,0,1,1)


答案下方的评论坚持使用for循环.看起来像


A comment below the answer insists on using a for-loop. That would look like

im = plt.imshow(np.random.randn(10,10))

plt.ion()
for i in range(100):

    A = np.random.randn(10,10)
    im.set_array(A)
    plt.pause(0.005)

plt.ioff()
plt.show()

它会比使用FuncAnimation慢一些,因为动画发生在GUI事件循环之外.另请注意,如在 Matplotlib中快速实时绘图中所示,针对这种情况实施blitting的工作量更大. /PyPlot

It's will be a bit slower than using FuncAnimation, because the animation happens outside the GUI eventloop. Also note that implementing blitting for such case is much more work as seen in Fast Live Plotting in Matplotlib / PyPlot

这篇关于使用plt.imshow可获得更快的刷新率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆