Kestrel
Kestrel

Reputation: 587

pyplot: Closing a show() window doesn't continue code

I have successfully been using pyplot to show heatmaps. Today it seems to have stopped working.

My problem is that setting up my plot, then calling the show() method shows the figure in a window, but when I close this window (by clicking the x at the top), my code doesn't continue past where the show() method is called. It seems to hang on show().

matrix is a numpy matrix.

This is an example of my code:

plt.pcolor(matrix, cmap=plt.cm.binary)
plt.xlabel('xaxis', fontsize=20)
plt.ylabel('yaxis', fontsize=20)
plt.axis([0, matrix.shape[1], 0, matrix.shape[0]])
plt.colorbar()
#This is where my code hangs...
plt.show()
#Closing the window manually does nothing.
#And the close() method doesn't seem to do anything.
plt.close()

After the show() method is called, and the window is closed, my process keeps going, and I have to manually terminate it.

Does anyone know the reason why this is happening?

Upvotes: 5

Views: 4243

Answers (4)

MacItaly
MacItaly

Reputation: 419

Just recently it looks as though you can do:

plt.show(block=False)

This will no longer block the code from continuing after the graph is closed.

Upvotes: 0

Brent Robertson
Brent Robertson

Reputation: 51

If you are using tkinter:

I ran into the same problem when using tkinter (to select the file with the data for the plots) in conjunction with pyplot. I have found that, by calling root.destroy on my tkinter.Tk() object, closing the window created by plt.show() allows my code to continue instead of hanging.

Upvotes: 4

Alon
Alon

Reputation: 303

because plt.show() is a "blocking function" and the code will not resume until you close the figure. it will not even get to plt.close() before you close the figure, as it is written after plt.show()

Upvotes: 0

tmdavison
tmdavison

Reputation: 69116

have you tried setting plt.ion() sometime before plt.show(). That should set interactive mode, and show won't stop the execution of your script.

Upvotes: 1

Related Questions