问题
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?
回答1:
Try starting Python using pythonw
instead of python
.
回答2:
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'
回答3:
Modify matplotlib.pyplot
import to:
import matplotlib
matplotlib.use('TkAgg') #----> Specify the backend
import matplotlib.pyplot as plt
回答4:
Add this in the beginning of your iPython:
% pylab inline
来源:https://stackoverflow.com/questions/30086663/plt-show-hangs-on-osx-with-anaconda-python