matplotlib: Error in sys.exitfunc - python

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

I had the same error. Using
matplotlib.pyplot.close()
at the end of my program fixed it. Perhaps this works for you?

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

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

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.

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

Related

Mouse, and keyboard python modules conflicting?

I am writing a test program which contains the mouse, and keyboard modules together, but they conflict when I run the program.
If applicable, I run Linux Mint 20.04 (Uma).
Here is my code:
import keyboard
import mouse
import time
time.sleep(2)
print("Finished part 1!")
x, y = mouse.get_position()
time.sleep(2)
print("Finished part 2!")
mouse.move(x, y)
time.sleep(2)
print("Finished part 3!")
keyboard.add_hotkey('ctrl+alt+c', mouse.move(x, y))
If I run this program normally, such as /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py in my terminal, it outputs:
Finished part 1!
Finished part 2!
Finished part 3!
Traceback (most recent call last):
File "/home/bhrz/Scripts/Python/Mouse/main.py", line 18, in <module>
keyboard.add_hotkey('ctrl+alt+c', mouse.move(x, y))
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 639, in add_hotkey
_listener.start_if_necessary()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 196, in init
_os_keyboard.init()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixkeyboard.py", line 109, in build_device
ensure_root()
File "/usr/local/lib/python3.8/dist-packages/keyboard/_nixcommon.py", line 174, in ensure_root
raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.
When I attempt to solve that error by entering this, sudo /bin/python3 /home/bhrz/Scripts/Python/Mouse/main.py, it outputs:
Traceback (most recent call last):
File "/home/bhrz/Scripts/Python/Mouse/main.py", line 2, in <module>
import mouse
ModuleNotFoundError: No module named 'mouse'
I have looked for answers, and some were to do the above, which as you can see, has resulted in failure, and another said to do sudo su then run the script--which also failed, with the same output as above.
Please help me figure out what the problem is.
I do have unintentionally 3 versions of python installed: 2.7, 3.8.10, and 3.9.5. I personally installed Python 3.9.5. I don't use 2.7, but it was installed with the python-dev
On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.
I have found the answer to my problem. The thread: Python can't find module when started with sudo 's second answer fixed my problem.
Instead of running sudo python3(/3.8) myScriptName.py, run sudo -E python3(/3.8) myScriptName.py.
Thank you, Nima for attempting to help me!
Use sudo /bin/python3.8 /home/bhrz/Scripts/Python/Mouse/main.py as you said:
On Python 3.9.5, the keyboard module isn't recognized, but on Python 3.8.10, it is.
python3 has been replaced when you installed python3.9.5. So it belongs to python 3.9.5. You need to use python3.8 in order to execute your script in python 3.8.10 environment.

Importing wxPython pops errors

Currently I am using Python 3.7, and Ubuntu 18.04. I downloaded wxPython from pip, but when I tried to import wx in my terminal, I get this error:
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/aleejandrof/anaconda3/lib/python3.7/site-packages/wx/__init__.py", line 17, in <module>
later on, when I tried other ways like and "._core import" I received an error like this:
ImportError: /home/aleejandrof/anaconda3/bin/../lib/libpangoft2-1.0.so.0: undefined symbol: pango_font_description_set_variations
After reading some posts here, I tried deleting the wx.py and wx.pyc files, which didn't work. The same happened when I read that downloading the main excecutable file would make the import occur with no errors, but it popped the same errors.
AttributeError: module 'wx' has no attribute '__version__'
I am trying to run a GUI pipeline, which works with wxPython. I'm thankful in advance, if any of you has suggestions.
When I made an environment of ubuntu18.04 using docker, ran into the same problem.
I had multiple libpangoft2-1.0.so.0 files.
It seemed the problem was /opt/conda/lib/libpangoft2-1.0.so.0 was looked up.
To change the name of the file solved the problem.
find / -name libpangoft2-1.0.so.0
/opt/conda/lib/libpangoft2-1.0.so.0
/opt/conda/pkgs/pango-1.42.4-h049681c_0/lib/libpangoft2-1.0.so.0
/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0
mv /opt/conda/lib/libpangoft2-1.0.so.0 /opt/conda/lib/libpangoft2-1.0.so.0-void
You may want to try:
conda install -c asmeurer pango

TclError matplotlib 1.5.0

Using the code below, a TclError is generated.
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20])
plt.show()
When I execute my script in terminal, I get the following:
/Users/<username>/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py:1035: UserWarning: Duplicate key in file "/Users/<username>/.matplotlib/matplotlibrc", line #2
(fname, cnt))
objc[44479]: Class TKApplication is implemented in both /Users/<username>/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[44479]: Class TKMenu is implemented in both /Users/<username>/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[44479]: Class TKContentView is implemented in both /Users/<username>/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[44479]: Class TKWindow is implemented in both /Users/<username>/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
2016-02-01 20:45:40.991 python[44479:21064918] setCanCycle: is deprecated. Please use setCollectionBehavior instead
2016-02-01 20:45:41.000 python[44479:21064918] setCanCycle: is deprecated. Please use setCollectionBehavior instead
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/<username>/anaconda/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
return self.func(*args)
File "/Users/<username>/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 283, in resize
self.show()
File "/Users/<username>/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 355, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "/Users/<username>/anaconda/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 30, in blit
id(data), colormode, id(bbox_array))
TclError
What is causing this error? I have tried adding backend: TkAgg to my matplotlibrc file, to no avail.
Please advise
Figured this out:
Instead of using:
matplotlib.use('TkAgg')
use:
matplotlib.use('Qt4Agg')
or any other backend
You can use 'TkAgg' but need to install Tkinter not with conda. The Anaconda packages for pil/pillow and matplotlib seem not to have TK properly included. Install pip with conda and then run pip install pillow matplotlib (Linux)
On windows you can use the packages from gohlke
http://www.lfd.uci.edu/~gohlke/pythonlibs/
install with --force-reinstall pillow matplotlib
then Tkinter TkAgg will work.

AttributeError: 'ZAxis' object has no attribute '_set_scale' error indicates matplotlib libraries in many places?! How to fix?

I'm getting the following error message when I try to plot a set of points in 3d using matplotlib:
Traceback (most recent call last):
File "/Users/r8t/summer-2014/diffusion-maps/diffusion1.py", line 55, in <module>
ax = fig.add_subplot(111, projection = '3d')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/figure.py", line 789, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 8457, in __init__
self._axes_class.__init__(self, fig, self.figbox, **kwargs)
File "/Library/Python/2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 91, in __init__
*args, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py", line 463, in __init__
self.cla()
File "/Library/Python/2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 1040, in cla
self.zaxis._set_scale('linear')
AttributeError: 'ZAxis' object has no attribute '_set_scale'
This seems to indicate that axes3d.p is in "/Library ... /axes3d.py", but self.cla() is in "/System/..."
I consulted python: AttributeError: 'ZAxis' object has no attribute '_set_scale'
where the comment said there were two copies of the library, and the asker seemed to figure out how to fix it. Can someone tell me how to fix it?
I'm running OS X 10.9.3, and recently updated, although I'm not sure that's relevant.
I tried to pip uninstall matplotlib, but I think it only removed the folders in "/Library...". I also tried to pip install again, but afterward got the same result.
Thanks!!!
Bobby
I'm working on mac os x 10.9.5.
I tried to solve this problem by specify the working path, but I failed.
Then I used brew to reinstall Python, and did following steps:
brew install python --framework --universal
cd /System/Library/Frameworks/Python.framework/Versions
sudo rm Current
sudo ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current
Finally, I succeeded. I can plot 3D figures using matplotlib.
Many thanks.
I think this is a dump of this and the problem is related with the upgrade. The paths got messed up. If you see your output, one line says
File "/Library/Python/2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py"
And the line above says:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/axes.py"
Some of those two Library folders is wrong.

have trouble with PIL to create PhotoImage object

I have Python 2.6 running on Fedora 13. I installed PIL 1.1.7, and I get the successful installed message in the Python prompt. I am able to import PIL.PhotoImage,but when I try to run the following, I get an error.
mgobj = PhotoImage(file=imgpath)
Stacktrace:
Traceback (most recent call last):
File "viewer-tk.py", line 25, in <module>
imgobj = PhotoImage(file=imgpath) # now JPEGs work!
File "/home/Toshiba/vinpython/venv/lib/python2.6/site-packages/PIL/ImageTk.py", line 116, in __init__
self.paste(image)
File "/home/Toshiba/vinpython/venv/lib/python2.6/site-packages/PIL/ImageTk.py", line 181, in paste
import _imagingtk
ImportError: No module named _imagingtk
In the module ImageTk.py , I see _imagingtk being imported,but i am not sure how do it get that module.Help is greatly appreciated!!!
You need to install Tkinter python module as stated at this trac. But since it's an integral part of python distributions. Since you are using fedora take a look at this page where you can install Tkinter when it doesn't come along, even being for Fedora 3 I think it might help.

Categories