Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot manually close matplotlib plot window

I'm running Python v2.7 and matplotlib v1.5.0 on Mac OS X Yosemite. Up to recently, I was able to run my script in the interactive interpreter, show a plot, and then manually kill the window. For example

import numpy as np
x = np.arange(1,10)
y = np.arange(1,10)
plt.plot(x,y)
plt.show()

This used to work fine. The window would close and I'd return to the prompt. However, now when I hit the red X to close the window, the window remains open. My command prompt returns and works fine, but the window is stuck and I see the spinning beach ball over it.

enter image description here

(Note that the cursor doesn't appear in the image, but you can see the red X is greyed out because I've hit close but it is stuck open).

It's not until I run the script a second time that the first window closes, but then I'm stuck with a second open plot with the same problem. I'm a bit confused because this only happened recently. Up till now, the window would close fine.

It's not a huge issue, because Python still runs and I can still make new plots, but I'm curious as to why the window would all of a sudden stick open. Any advice?

UPDATE

I solved the problem by switching the interactive backend. Either Qt4Agg or TkAgg as an interactive backend resolves the issue. But the question remains why the macosx and CocoaAgg backends show this behavior.

like image 863
Nate Avatar asked Dec 05 '15 17:12

Nate


People also ask

How do you stop a matplotlib plot?

Simply call plt. close() at the end of each plot instead of plt. show() and they won't be displayed.

What does PLT close () do?

plt. close() closes a window, which will be the current window, if not specified otherwise.

Is PLT show () blocking?

show() and plt. draw() are unnecessary and / or blocking in one way or the other.


1 Answers

For a permanent solution (I'd rather not have to switch backends every time I open ipython) you can modify matplotlibrc.

There are various matplotlibrc files that can be changed (i.e. for just a local directory, or globally, etc.). To find the configuration file that's been loaded for your ipython session, use matplotlib.matplotlib_fname(). Here's what I got:

In [1]: import matplotlib

In [2]: matplotlib.matplotlib_fname()
Out[2]: u'/usr/local/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'

Opening the rc file and changing the line:

backend      : macosx

to read:

backend      : Qt4Agg

did the trick for me.

like image 157
mattsilver Avatar answered Oct 19 '22 17:10

mattsilver