34

At the end of the last function I call in one of my programs, I have the following code to plot a simple color plot.

plt.figure()
plt.pcolormesh(X,Y,Z)
plt.colorbar()
plt.show()

Afterwords I return to main and my program is complete. The plot displays as expected, however when I go to close it using the x button in the corner (on ubuntu), my program doesn't end. It just hangs there with a process running. How can I correct this?

user1764386
  • 5,311
  • 9
  • 29
  • 42
  • A quick and dirty solution might be to try using `plt.draw()` instead of `plt.show()`. `show()` loads all of the interactive tools, where `draw()` should be a simple viewer which may play more nicely with the rest of your program. – John Lyon Nov 26 '12 at 02:06
  • Also, which version of matplotlib are you using? Version 1.0.0 made some changes to `show()` to make it more compatible with what you want to do. [See the docs](http://matplotlib.org/faq/howto_faq.html#use-show) for more info. – John Lyon Nov 26 '12 at 02:10
  • 2
    Are you using ipython, or some other matplotlib friendly console which handles the GUI threads correctly? – DaveP Nov 26 '12 at 02:11

2 Answers2

34

your matplotlib might be running in non-interactive mode for some reason. I am not sure how to prevent that in your local configuration but if you add either this:

plt.ion()

or this:

matplotlib.interactive(True)

somewhere at the beginning of your script, it should change the behaviour of your plots.

Luc
  • 158
  • 6
Zak
  • 3,063
  • 3
  • 23
  • 30
4

For interactive mode, You need this at the head of file:

import matplotlib
matplotlib.use("TkAgg")
iceflame
  • 609
  • 5
  • 8
  • You should write this code before importing plt as cited in the answer to this question https://stackoverflow.com/questions/55811545/importerror-cannot-load-backend-tkagg-which-requires-the-tk-interactive-fra – BHA Bilel May 10 '20 at 19:57