12
import matplotlib.pyplot as plt
plt.figure()
plt.show()

...crickets. The interpreter hangs and I can't seem to interrupt it.

This is with Python 2.7.9 and Anaconda 2.2.0 (x86_64) on OSX.

Does this happen for anyone else? How can I solve this problem?

James Atwood
  • 4,289
  • 2
  • 17
  • 17
  • 1
    Are you sure it's `plt.show()` that hangs and not the import? Because there's an issue with the font cache on OS X which is addressed in the answer to [this question](http://stackoverflow.com/questions/17490444/import-matplotlib-pyplot-hangs). – Lukas Graf May 06 '15 at 20:15
  • 1
    The import works fine, and so far as I can tell, so does everything else. The hang definitely happens with `plt.show()` – James Atwood May 06 '15 at 20:24
  • It runs fine with me, it took a few seconds before the window popped up. Do you use interactive or non-interactive mode? And what is your back-end? Do you run these lines from a script in the command line, or do you type them in the python interpreter? In my experience with OS-X, windows created from the command line often do not rise to the front. Could it be that the plot window is created behind other windows or hidden in a corner? – titusjan May 06 '15 at 22:16
  • 1
    Nope - no window anywhere. The backend is 'MacOSX'. Definitely hangs. – James Atwood May 07 '15 at 01:45
  • 1
    Ah, @titusjan makes an excellent point. Now that he mentioned the backend, I remember that I once had a similar issue that was resolved once I changed the backed to `backend: TkAgg` in my `~/.matplotlib/matplotlibrc`. You may want to give that a shot as well if using `pythonw` isn't too convenient for you. – Lukas Graf May 07 '15 at 20:05

4 Answers4

13

Try starting Python using pythonw instead of python.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • I don't know why this worked, but it did. Thank you! – James Atwood May 07 '15 at 19:53
  • 3
    On OS X, you need to use `pythonw` whenever you use something that hooks into the OS X windowing system (such as matplotlib), as it needs to "trick" OS X into thinking it is a Framework (using the `python.app` package that you'll find in the `anaconda` directory). – asmeurer May 08 '15 at 18:36
  • 1
    Another thing that should work is to use `ipython`, as it uses python.app automatically. – asmeurer May 08 '15 at 18:37
  • Using `ipython` worked for me, thanks @asmeurer. For scripts, I changed the hashbang line to `#!/usr/bin/env ipython` and it works like a charm. – A.Wan Nov 04 '15 at 22:37
0

This is most likely an issue with your backend setting. If you want your plots to show up inline (inside of your notebook) import with this added line:

import matplotlib.pyplot as plt
%matplotlib inline

To have your plots show up interactively inline (inside your notebook) import with this line instead:

import matplotlib.pyplot as plt
%matplotlib notebook

To have your plots show up outside of your notebook (in a new window), import with this line instead:

import matplotlib.pyplot as plt
%matplotlib qt

NOTE: You need to restart the kernel to switch between inline notebook and outside qt to avoid the error: Warning: Cannot change to a different GUI toolkit

Finally, if issues persist, the following might help uncover what is going on:

import matplotlib
matplotlib.get_backend()

If using any of the options above, it should output one of the following:

  • 'module://ipykernel.pylab.backend_inline'
  • 'nbAgg'
  • 'Qt4Agg'
wellplayed
  • 943
  • 8
  • 16
0

Modify matplotlib.pyplot import to:

import matplotlib
matplotlib.use('TkAgg') #----> Specify the backend
import matplotlib.pyplot as plt
Ari
  • 7,251
  • 11
  • 40
  • 70
-1

Add this in the beginning of your iPython:

% pylab inline
das-g
  • 9,718
  • 4
  • 38
  • 80
Ahmad
  • 1