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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.