Matplotlib backend GTK3 Agg using Cairo? - python

I don't understand why while I' trying to use the Gtk3Agg backend and I end up with an error telling me the cairo module is not found.
** (simple_plot_in_gtk3.py:312517): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files
Traceback (most recent call last):
File "simple_plot_in_gtk3.py", line 5, in
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
File "/Produits/publics/x86_64.Linux.RH6/python/3.4.1/lib/python3.4/site-packages/matplotlib-1.3.1-py3.4-linux-x86_64.egg/matplotlib/backends/backend_gtk3agg.py", line 1, in
import cairo
ImportError: No module named 'cairo'
I'm trying to run the matplotlib/GTK3 example from matplolib site.

Installing Python binging for Python works:
$ pip install pycairo

If you look at the backend_gtk3agg.py file, you'll see that the first lines in the file are;
import cairo
import numpy as np
import sys
import warnings
import backend_agg
import backend_gtk3
from matplotlib.figure import Figure
from matplotlib import transforms
So, yes, the gtk3agg backend requires the cairo library and its Python bindings. And numpy.
It uses Cairo for ImageSurface buffers, because that is something AGG does not provide. I do not know why the gtk3agg developers chose this method. Presumably because it was convenient.

Related

Issue converting python script with pyinstaller. Import error: Incompatible library

I am relatively new to programming and I am using macOS Catalina 10.15.4 python 3.7 and pyinstaller3.6 to convert my python script to an executable. I manage to convert the script to an executable but when I click on it I get the following error:
issue:Traceback (most recent call last):
File "QC.py", line 2, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "cv2/__init__.py", line 5, in <module>
ImportError: dlopen(/Users/kapten42/Desktop/OCR/QA/dist/QC/cv2/cv2.cpython-37m-darwin.so, 2): Library not loaded: #loader_path/libpng16.16.dylib
Referenced from: /Users/kapten42/Desktop/OCR/QA/dist/QC/libfreetype.6.dylib
Reason: Incompatible library version: libfreetype.6.dylib requires version 54.0.0 or later, but libpng16.16.dylib provides version 38.0.0
[35604] Failed to execute script QC
I installed most of my packages using pip3 except for tesseract which I had to install using brew. I have been searching everywhere and really stumped with this one. I used brew to update libpng but the it didn't change anything. Below is the list of imports made at the top of my code. I am really stuck so any advice is greatly appreciated
from PIL import Image
import cv2
from skimage.filters import threshold_local
from skimage import io
import csv
import pytesseract
import re
import imutils
import numpy as np
from datetime import datetime
from spellchecker import SpellChecker
import json
import tempfile
import requests
import os
from pdf2image import convert_from_path
from dateutil.relativedelta import relativedelta
So I found a solution to the problem. Pyinstaller was pulling the wrong libpng16.16.dylib into the dist folder that was created. I used
brew install libpng
to get the latest version and then went to the folder
usr/local/cellar/libpng/1.6.37/lib
and copied libpng16.16.dylib and moved it to the dist folder and that resolved the issue.

importError tkinter when importing matplotlib

I am unable to import matplotlib on this deep learning AMI from aws marketplace
import matplotlib.pyplot as plt
And I get this error
ImportError: No module named 'tkinter'
What I've tried (and the errors returned):
pip install tkinter
> No matching distribution found for TKinter
sudo yum install tkinter
> No package tkinter available.
> Error: Nothing to do
I personnally had this kind of troubles with tkinter, before to realize it requires a 't' in python3 and a 'T' in python2. so when I need compatibility my codes contain:
import sys
if sys.version_info[0]>2:
# for Python 3
import tkinter as tk
else:
# for Python 2
import Tkinter as tk
I hope this help
First find location of matplotlibrc file
import matplotlib
matplotlib.matplotlib_fname()
u'/usr/local/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc'
Then go to this file and change the current configuration of backend to
backend : agg
This does not solve the problem of import Tkinter, but it does allow the import of pyplot
This worked for me:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pylab as plt
Had the same problem after updating to new matplotlib 2.x on python 3.5.x

python opencv import error for 2.7

I tried to import the cv2 package in windows and I got the error:
>>> import numpy
>>> numpy.version.version
'1.6.1'
>>> import cv2
RuntimeError: module compiled against API version 0xa but this version of numpy is 0x6
Traceback (most recent call last):
File "", line 1, in
import cv2
ImportError: numpy.core.multiarray failed to import
I have also tried numpy verson 1.11.1 but it didn't work.
You should ensure that you have a single version of numpy installed, assuming you are not working within a virtualenv. Python may still be loading the old numpy package. If that is the case, you will need to remove the old version, or, to make things easier in the long run, use a virtualenv with only the packages you require.
You can check the numpy path:
import numpy
print numpy.__path__

Calling C++ code from Kivy 1.8.0

I have some numeric code in C++ that I want to call from Python (2.7) and Kivy (1.8,0). The C++ code is in twocomp.dll and I have written an interface module utilizing pycxx and compiled and linked this interface into pycLink.pyd. I can call this link and the dll successfully from a library module scoppylib.py:
import pycLink
import math
import sys
import numpy as np
import matplotlib.pyplot as plt
...
I have called this Python library successfully from Python programs run from a command line and from inside ipython notebook.
However, when I call the same library from a kivy program, I get the following error:
"Traceback (most recent call last):
File "C:\users\Mailen\SCoPPY\kivy\scop.py", line 9 in module
import scoppylib
File "C:\users\Mailen\SCoPPY\kivy\scooppylib.py", line 7, in module
import pycLink
ImportError: DLL load failed: The specified procedure could not be found."
I concluded that the interface pycLink.pyd was not found because of the environment change for kivy, so tried changing PYTHONPATH and tried placing pycLink.pyd in various related directories, but nothing changes the resulting error.
Any suggestions would be welcomed!

matplotlib "DLL load failed" when import pylab

I'm new to matplotlib. My environment is WinXP, PythonWin 2.6.2, NumPy 1.3.0, matplotlib 0.98.5.3.
>>> import matplotlib.pylab as pylab
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "D:\Python26\lib\site-packages\matplotlib\pylab.py", line 253, in <module>
from matplotlib.pyplot import *
File "D:\Python26\lib\site-packages\matplotlib\pyplot.py", line 75, in <module>
new_figure_manager, draw_if_interactive, show = pylab_setup()
File "D:\Python26\lib\site-packages\matplotlib\backends\__init__.py", line 25, in pylab_setup
globals(),locals(),[backend_name])
File "D:\Python26\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 8, in <module>
import tkagg # Paint image to Tk photo blitter extension
File "D:\Python26\lib\site-packages\matplotlib\backends\tkagg.py", line 1, in <module>
import _tkagg
ImportError: DLL load failed: cannot find the module
I searched the web and it's said because lack of msvcp71.dll, but there is already one in my C:\windows\system32\
anyone can help?
Thanks.
try this, before using any other module
import matplotlib
matplotlib.use('Agg')
import matplotlib.pylab as pylab
see http://www.mail-archive.com/matplotlib-users#lists.sourceforge.net/msg05372.html for more details and other ways
matplotlib can use different backends for rendering, agg is pure draw with no UI, so you can only save images e.g.
plt.savefig("plt.png")
read http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend for more details, e.g. you can output to ps, pdf, wxagg, tkagg etc, so if you have wxpython installed use this
matplotlib.use('wxagg')
also i think tkagg backend should have been work, as tkinter(http://wiki.python.org/moin/TkInter) is default gui with python, did you install python from python.org?
I had the same problem installing activestat python 2.6 and pylab. After installing the sourceforge python 2.6.2 however it worked fine
I actually found the answer and the graphs are running great on my computer. If you are getting a DLL error like this, try downloading msvcp71.dll and msvcr71.dll in your computer and then copy paste these two in your System32 folder:
C:\Windows\System32
and also copy-paste these two dll's in SysWOW64 folder if you are working on 64bit operating System
C:\Windows\SysWOW64
Now try running your code file in Python and it will load the graph in couple of seconds. Here is the link which says how to copy-paste dll's to both folder, this might help
http://www.youtube.com/watch?v=xmvRF7koJ5E
Cheers...
I had this issue using iPython notebooks using Python 2.7. Apparently the latest Seaborn update does not play well with my local configuration settings, so I did "conda/pip install seaborn=0.5.1". It downgraded the necessary packages automatically, and my problem went away.

Categories

Resources