使用plt.imshow可获得更快的刷新率

编程入门 行业动态 更新时间:2024-10-23 10:19:19
本文介绍了使用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?

注意:

  • 我已经尝试了 Matplotlib中的快速实时绘图的主要答案/PyPlot ,但是对于这种简单的任务来说,这似乎是一种复杂的方法(使用blit参数),而且我不会得到28 fps,而只能得到15 fps .

  • 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.

我只想将矩阵显示为图像:没有边框,没有轴,没有子图等,我想这可以比解决方案在Matplotlib/PyPlot中进行快速实时绘图,也许不是使用matplotlib而是使用另一个库?

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,使用imshow刷新图像更快
  • 相当于pygame翻转的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可获得更快的刷新率

本文发布于:2023-11-30 03:25:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:刷新率   更快   可获得   plt   imshow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!