houbysoft
houbysoft

Reputation: 33430

Close pyplot figure using the keyboard on Mac OS X

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.

Upvotes: 16

Views: 6124

Answers (4)

sacuL
sacuL

Reputation: 51425

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

Upvotes: 0

Danny Sullivan
Danny Sullivan

Reputation: 3854

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

Upvotes: 13

houbysoft
houbysoft

Reputation: 33430

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

Upvotes: 16

DrV
DrV

Reputation: 23550

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.

Upvotes: -2

Related Questions