16

Is there a way to close a pyplot figure in OS X using the keyboard (as far as I can see you can only close it by clicking the window close button)?

I tried many key combinations like command-Q, command-W, and similar, but none of them appear to work on my system.

I also tried this code posted here:

#!/usr/bin/env python

import matplotlib.pyplot as plt

plt.plot(range(10))

def quit_figure(event):
    if event.key == 'q':
        plt.close(event.canvas.figure)

cid = plt.gcf().canvas.mpl_connect('key_press_event', quit_figure)

plt.show()

However, the above doesn't work on OS X either. I tried adding print statements to quit_figure, but it seems like it's never called.

I'm trying this on the latest public OS X, matplotlib version 1.1.1, and the standard Python that comes with OS X (2.7.3). Any ideas on how to fix this? It's very annoying to have to reach for the mouse every time.

houbysoft
  • 32,532
  • 24
  • 103
  • 156

4 Answers4

16

This is definitely a bug in the default OS X backend used by pyplot. Adding the following two lines at the top of the file switches to a different backend that works for me, if this helps anyone else.

import matplotlib
matplotlib.use('TKAgg')
houbysoft
  • 32,532
  • 24
  • 103
  • 156
13

I got around this by replacing

plt.show()

with

plt.show(block=False)
input("Hit Enter To Close")
plt.close()

A hack at its best, but I hope that helps someone

Danny Sullivan
  • 3,626
  • 3
  • 30
  • 39
0

use interactive mode:

import matplotlib.pyplot as plt
# Enable interactive mode:
plt.ion()

# Make your plot: No need to call plt.show() in interactive mode
plt.plot(range(10))

# Close the active plot:
plt.close()
# Plots can also be closed via plt.close('all') to close all open plots or
# plt.close(figure_name) for named figures.

Checkout the "What is interactive mode?" section in this documentation

Interactive mode can be turned off at any point with plt.ioff()

sacuL
  • 49,704
  • 8
  • 81
  • 106
-2

When you have focus in the matplotlib window, the official keyboard shortcut is ctrl-W by this:

http://matplotlib.org/1.2.1/users/navigation_toolbar.html

As this is a very un-Mac way to do things, it is actually cmd-W. Not so easy to guess, is it?

If you are using an interactive shell, you can also close the window programmatically. See:

When to use cla(), clf() or close() for clearing a plot in matplotlib?

So, if you use pylab or equivalent (everything in the same namespace), it is just close(fig). If you are loading the libraries manually, you need to take close from the right namespace, for example:

import matplotlib.pyplot as plt

fig = plt.figure()
plt.plot([0,1,2],[0,1,0],'r')
fig.show()
plt.close(fig)

The only catch here is that there is no such thing as fig.close even though one would expect. On the other hand, you can use plt.close('all') to regain your desktop.

Community
  • 1
  • 1
DrV
  • 22,637
  • 7
  • 60
  • 72
  • As mentioned in my question, I already tried those... none work for me (neither cmd-W nor ctrl-W) – houbysoft Jun 23 '14 at 19:02
  • @edit: yeah I know about those, but those don't help relieve the annoyance. Basically I want it to popup a graph, I'll stare at it for a while and then I need a key combo that will just close it without having to use the mouse. I can't use close(fig) if I don't have a way to listen for key events (as mentioned in the question I tried adding a event handler) – houbysoft Jun 23 '14 at 19:05
  • I am afraid your problem is that the focus is not in the plot window. Do the key shortcuts work, if you move the focus? If so, then cmd-Tab + cmd-W... – DrV Jun 23 '14 at 19:07
  • focus is definitely in the window; for example if I hit tab it cycles over the buttons in the window on the bottom. – houbysoft Jun 23 '14 at 19:10
  • It seems like a bug to me, because the snippet I posted is supposed to work, but it doesn't – houbysoft Jun 23 '14 at 19:10
  • I agree, because all these tricks seem to work for me on a mac (IPython shell with macports python.) The standard OS X python seems to create more problems than solve, at least in scientific use. – DrV Jun 23 '14 at 19:11