5

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?

1
  • 1
    Are you using this in ipython? What OS are you working on? Have you changed anything in your script or updated any packages that could have caused the problem? Could you provide a bit more details? Commented Jun 9, 2015 at 12:55

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

When I add plt.ion() before plt.show(), the window appears briefly (long enough to see the figure, actually), and then closes by itself.
@Kestrel Well in that case it closes because you call plt.close().
This solution is a work-around, but not an answer to the question why show continues to block after closing the figure.
Agreed, just thought it might be useful in this case
@hitzg even when plt.close() is removed, it still exhibits the same blocking behaviour.
|
0

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()

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.