关闭每次迭代中的matplotlib图

发布于 2025-02-11 14:58:55 字数 325 浏览 1 评论 0原文

我有一个循环,需要在每次迭代中显示一个数字,我需要完全关闭图形,并在一段时间后显示另一个数字。这是我的代码:

 for j in range (3):
   img = mpimg.imread("E" + str(j) + ".jpg")
   imgplot = plt.imshow(img)
   plt.show(block=False)
   plt.pause(3)
   plt.close()

这里似乎每个数字都会用另一个数字替换,尽管我需要代码才能完全关闭第一个数字,进行一些操作然后显示另一个数字。 ),但它不起作用。
任何帮助将不胜感激。

I have a loop and need to show a figure in each iteration, I need to close the figure completely and after a while show another figure. This is my code:

 for j in range (3):
   img = mpimg.imread("E" + str(j) + ".jpg")
   imgplot = plt.imshow(img)
   plt.show(block=False)
   plt.pause(3)
   plt.close()

Here it seems that each figure will replace with another figure although I need the code to close the first figure completely, do some operations and then show another figure.I even used plt.close("all") but it didn't work.
Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2025-02-18 14:58:55

我不明白您的确切期望,但是添加plt.pause(1)可以具有相似的结果。

如果您的PLT后端是QT(您可以通过plt.get_backend())进行检查,以进行数字的绝对定位,请使用plt.get_current_fig_manager()。 Y0,宽度,高度)函数。
此外,如果您希望某些操作控制开口和关闭数字,则插入,而带有键盘输入的循环可能是一个简单的灵魂。

%matplotlib qt # Only if you are using Jupyter


import keyboard
import time

def keyboard_block(key):
    while(True):
        time.sleep(1.0)
        key_input = keyboard.read_key()
        if key_input == key:
            break
        else:
            print(key_input)

for j in range (3):
   img = mpimg.imread("E" + str(j) + ".jpg")
   imgplot = plt.imshow(img)
   plt.get_current_fig_manager().window.setGeometry(600,400,400,400)
   plt.show(block=False)
   plt.pause(3)
   keyboard_block(key='a') # wait before closing
   plt.close()
   keyboard_block(key='a') # wait before opening a new one

在此代码中,第一个数字posp上升了3秒,它将在1秒内关闭。然后第二个弹出。

I don't understand what you exactly expect, but adding plt.pause(1) can have a similar result.

If your plt backend is Qt (you can check it via plt.get_backend() ), for absolute positioning of the figures, please use plt.get_current_fig_manager().window.setGeometry(x0,y0,width,height) function.
Additionally, if you want some operations to control the opening and closing figures, inserting while loop with keyboard input can be a simple soultion.

%matplotlib qt # Only if you are using Jupyter


import keyboard
import time

def keyboard_block(key):
    while(True):
        time.sleep(1.0)
        key_input = keyboard.read_key()
        if key_input == key:
            break
        else:
            print(key_input)

for j in range (3):
   img = mpimg.imread("E" + str(j) + ".jpg")
   imgplot = plt.imshow(img)
   plt.get_current_fig_manager().window.setGeometry(600,400,400,400)
   plt.show(block=False)
   plt.pause(3)
   keyboard_block(key='a') # wait before closing
   plt.close()
   keyboard_block(key='a') # wait before opening a new one

In this code, the first figure posp up for 3 seconds and it will close in 1 second. Then the second one pops up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文