matplotlib: Error in sys.exitfunc
I keep getting error in sys.exitfunc
when working with matplotlib. For example, the following code throw it for matplotlib 1.3.0 / Python 2.7.3 / Ubuntu 12.04.3 LTS
from matplotlib.pyplot import figure, show
from numpy.random import random
fh = figure(figsize = (15, 10, ))
ax = fh.add_axes((.1, .1, .8, .8, ))
ax.scatter(random((100, )), random((100, )))
fh.show()
This yields
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/_pylab_helpers.py", line 86, in destroy_all
manager.destroy()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py", line 427, in destroy
self.canvas.destroy()
AttributeError: FigureManagerGTK3Agg instance has no attribute 'canvas'
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/_pylab_helpers.py", line 86, in destroy_all
manager.destroy()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_gtk3.py", line 427, in destroy
self.canvas.destroy()
AttributeError: FigureManagerGTK3Agg instance has no attribute 'canvas'
This happens any time the program terminates without show()
, including when an unrelated error is raised.
If I use show()
instead of fh.show()
, I don't get this error. I could just do that, but this error pops up in a lot of places and I prefer to just solve it (and I want to be able to exit without showing a figure).
I tried other backends which either are unavailable, don't have show or give the same error (this is GKT3Agg).
Asked by: Lyndon630 | Posted: 06-12-2021
Answer 1
I had the same error. Using matplotlib.pyplot.close() at the end of my program fixed it. Perhaps this works for you?
Answered by: Arthur230 | Posted: 07-01-2022Answer 2
plt.plot(return_array, risk_array)
plt.title('Pareto Front for '+ r'$\lambda \in$ [0.0, 1.0]')
plt.xlabel('Return')
plt.ylabel('Risk')
plt.axis([0.02, 0.05, 0.01, 0.016])
only using above code gives error but adding
matplotlib.pyplot.show()
in the end works for me
Answered by: Maddie387 | Posted: 07-01-2022Answer 3
dowload matplotlib-1.3.1.tar.gz from http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.3.1/
cd matplotlib-1.3.1
sudo python setup.py install
before that install following
sudo apt-get install libfreetype6-dev
sudo apt-get install python-dev
sudo apt-get install libevent-dev
sudo apt-get install libpng-dev
Answered by: Caroline310 | Posted: 07-01-2022
Answer 4
I had the same error and the solution of ShikharDua solved the problem for me. My system: ubuntu 13.10 64 bit.
I wanted to run an example code which didn't call the plt.show()
function at the end resulting in:
AttributeError: FigureManagerGTK3Agg instance has no attribute 'canvas'
Interestingly, the same code, without the plt.show()
, worked in ipython if pylab
is invoked before.
edit: Ok, i just read, that you mentioned this in your question already. So, this not really a solution for you. However, it is a solution for beginners who were wondering why some example codes don't work.
Answered by: Maddie725 | Posted: 07-01-2022Answer 5
This is my horrible hack to get around it:
import matplotlib.pyplot as plt
# your use of matplotlib here where you don't use the plot.show() function
try:
plt.close()
except AttributeError, e:
pass
Answered by: David724 | Posted: 07-01-2022
Similar questions
python - How to skip sys.exitfunc when unhandled exceptions occur
As you can see, even after the program should have died it speaks from the grave. Is there a way to "deregister" the exitfunction in case of exceptions?
import atexit
def helloworld():
print("Hello World!")
atexit.register(helloworld)
raise Exception("Good bye cruel world!")
outputs
Traceback (most recent call last):
File "test.py", line 8, in <module>
ra...
sys.exitfunc not working in python
I am trying to run following simple code
import sys
print("Starting Test Python Module");
def testmethod():
print("From test method")
sys.exitfunc = testmethod
print("Terminating Test Python Module");
and it prints
C:\Users\athakur\Softwares>python test.py
Starting Test Python Module
Terminating Test Python Module
I am not able to understand why ...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python