Using show () and close () from matplotlib

I'm having some problems with matplotlib .... I cannot open 2 windows at once to display the image with show (), it seems that the script stops at the line that I use show, and do not continue if I do not close the display manually . Is there a way to close the figure window inside the script?

The following code does not start as I want:

import matplotlib.pyplot as plt from time import sleep from scipy import eye plt.imshow(eye(3)) plt.show() sleep(1) plt.close() plt.imshow(eye(2)) plt.show() 

I expected the first window to close after 1 second, and then the second will open, but the window does not close until I close it. Am I doing something wrong, or is it the way it should be?

+7
source share
2 answers

plt.show () is a blocking function.

Essentially, if you want to open two windows at once, you need to create two digits, and then use plt.show () at the end to display them. In fact, the general rule is that you customize your stories, and plt.show () is the last thing you do.

So in your case:

 fig1 = plt.figure(figsize=plt.figaspect(0.75)) ax1 = fig1.add_subplot(1, 1, 1) im1, = plt.imshow(eye(3)) fig2 = plt.figure(figsize=plt.figaspect(0.75)) ax2 = fig2.add_subplot(1, 1, 1) im2, = plt.imshow(eye(2)) plt.show() 

You can switch between graphs using axes(ax2) .

I have put together a detailed example that demonstrates why the plot function blocks and how it can be used in the answer to another question: https://stackoverflow.com/a/416829/

+11
source

I am using PyScripter and Python 2.7, as well as the plt.show() problem of blocking all executions until you manually close the numbers.

I found that changing the Python engine to "remote (Wx)" allows the script to run after plt.show() - so you can close the numbers with plt.close() .

+1
source

All Articles