vzaretsk
vzaretsk

Reputation: 81

matplotlib crashing Python

I'm new to Python and matplotlib. A simple script I wrote is crashing and I was able to reproduce the crash with the following code:

import matplotlib.pyplot as plt
plt.figure(1)
plt.figure(2)
#plt.show()

The error is python.exe has stopped working. If I uncomment the plt.show(), it still crashes depending on the order I close the plots (no crash if 2 is closed first, crash if 1 is closed first). I'm using Windows 7, Python 3.4, and I installed the individual modules from www.lfd.uci.edu/~gohlke/pythonlibs/. Do I have something configured incorrectly or a misunderstanding of how to use matplotlib?

Upvotes: 8

Views: 17854

Answers (5)

Joe
Joe

Reputation: 1045

For macOS, just make sure that

~/.matplotlib/matplotlibrc contains:

backend: MacOSX

You don't need the other backends unless you specifically want them. Alternatively, perhaps you can do:

import matplotlib
matplotlib.use("MacOSX")

though I have not tested that.

Upvotes: 0

user1610278
user1610278

Reputation: 19

I was having this issue, I thought it was some line in my code causing the bug, but in fact the very act of importing matplotlib.pyplot was killing my program. I solved it by first running it in verbose mode:

python -v [programname].py

This shows the last action the importer does before crashing. For me, the last line of this was:

import 'PyQt5' # <_frozen_importlib_external.SourceFileLoader object at 0x000001F8EC9C0908>

This suggested to me that the dependent library PyQt5 was causing issues, so I ran pip install PyQt5, and magically everything started working.

Upvotes: 1

Luis B
Luis B

Reputation: 1722

You need to set the TkAgg backend explicitly. With the following code, the problem is resolved.

import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt

Note that setting the TkAgg backend after importing pyplot does not work either; it crashes too. You need to set it before importing pyplot.

Upvotes: 6

dbbudd
dbbudd

Reputation: 21

I had a similar issue in OSX when I updated to Python 3.4. IDLE was also crashing and there was a warning telling me the version was unstable.

I solved it by following the prompts and updating the version of Tcl/Tk (8.5.9) - http://www.python.org/download/mac/tcltk .

Upvotes: 0

Venkatesh
Venkatesh

Reputation: 3789

This could be issue with python 3.x

I have tried with python 2.7 on my windows machine and it works perfectly fine!

You can either downgrade your python to 2.7 or if you feel its too late to do why dont you give it a try to call close()

Import matplotlib
matplotlib.use('wxAgg')
Import matplotlib.pyplot as plt
# your scripts
plt.close('all')

Upvotes: 0

Related Questions