I am having trouble getting my matplotlib library to work. I am running it on a virual environment, and I have installed numpy, and matplotlib. I first open my virtual environment, activate python, then I enter,
import matplotlib
everything goes fine, but then I enter,
import matplotlib.pyplot as plt
I get the error,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
I have tried a number of different solutions to no avail. What could be the problem?
Use the below mentioned commands:
import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt
This worked for me. I hope it helps.
Related
I'm new to Jython and PyDev. I need to run my Python code in Eclipse.
The Python code works in Spyder/Jupyter Notebooks. When I run the programm as Jython Run/Python Run, an error pops up.
How can I solve this problem?
console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
C:\Users\Hai Mi\Anaconda3\Lib\site-packages\numpy\__init__.py:140: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "C:\Users\Hai Mi\workspace\Camunda\learning_classify.py", line 7, in <module>
import nltk
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\nltk\__init__.py", line 129, in <module>
from nltk.collocations import *
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\nltk\collocations.py", line 40, in <module>
from nltk.metrics import ContingencyMeasures, BigramAssocMeasures, TrigramAssocMeasures, QuadgramAssocMeasures
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\nltk\metrics\__init__.py", line 16, in <module>
from nltk.metrics.scores import (
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\nltk\metrics\scores.py", line 18, in <module>
from scipy.stats.stats import betai
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\nltk\metrics\scores.py", line 18, in <module>
from scipy.stats.stats import betai
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\scipy\__init__.py", line 62, in <module>
from numpy import show_config as show_numpy_config
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
from . import overrides
File "C:\Users\Hai Mi\Anaconda3\Lib\site-packages\numpy\core\overrides.py", line 166
SyntaxError: unqualified exec is not allowed in function 'decorator' because it contains free variables
Looks like a similar issue was discussed here
How do I fix UnsupportedCharsetException in Eclipse Kepler/Luna with Jython/PyDev?
and adding
-Dpython.console.encoding=UTF-8
to the Run Configurations -> VM arguments could be a solution.
I have set up a virtual environment and when I try to run matplotlib on it, I get the following error:
Traceback (most recent call last):
File "/Users/sumeet/PycharmProjects/VISM/test2.py", line 2, in <module>
import matplotlib.pyplot as plt
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
[backend_name], 0)
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
from . import tkagg # Paint image to Tk photo blitter extension.
File "/Users/sumeet/tensorflow/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
from six.moves import tkinter as Tk
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
From what I understand, I need to
set the environment variables correctly in the activate script
or I can symlink tkinter folder to virtualenv python2.7.
I have followed the steps here: TKinter in a Virtualenv, but I am still getting the error.
Steps that I followed are:
To virtualenv/bin/activate, I added
TK_LIBRARY=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib
TKPATH=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib
TCL_LIBRARY=/usr/lib
export TCL_LIBRARY TK_LIBRARY TKPATH
In virtualenv/lib/python2.7 I ran, ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_tkinter.so
Note that when I am using the system Python as an interpreter on PyCharm, matplotlib works fine which gives me a feeling that Tkinter is available somewhere on my system but I am failing to call it from my virtual environment.
The link above seems to be working for Ubuntu. I am wondering if anyone know what is the analogous version for Mac.
Thanks!
If you are not specific to using tkinter in virtual environment then you can use the following lines of code:
import matplotlib
matplotlib.use(‘TkAgg’)
import matplotlib.pyplot as plt
I hope this solves the problem.
I have those errors when i'm trying to import the matplotlib.
How can i fix this?
I have install matplotlib with the pip install matplotlib and this was a succes.
But when i import the package i have this kind of error.
i see it is in the init block but how can i fix this?
i have already tried the pip matplotlib upgrade but this keeps the same problem.
i just have matplotlib uninstalled and back installed and it is the same problem.
My code:
import matplotlib.pyplot as plt
Error:
Traceback (most recent call last):
File "C:\Python27\TEST.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 26, in <module>
from matplotlib.figure import Figure, figaspect
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 24, in <module>
import matplotlib.artist as martist
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 7, in <module>
from transforms import Bbox, IdentityTransform, TransformedBbox, \
File "C:\Python27\lib\site-packages\matplotlib\transforms.py", line 35, in <module>
from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
ImportError: DLL load failed: %1 is geen geldige Win32-toepassing
I have tried another code, that I found in the comments. But this generate my another error code.
Code :
import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
Error:
Traceback (most recent call last):
File "C:\Python27\TEST.py", line 1, in <module>
import matplotlib as mpl
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1097, in <module>
rcParams = rc_params()
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 933, in rc_params
fname = matplotlib_fname()
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 781, in matplotlib_fname
for fname in gen_candidates():
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 770, in gen_candidates
yield os.path.join(six.moves.getcwd(), 'matplotlibrc')
AttributeError: '_MovedItems' object has no attribute 'getcwd'
Error
Little test program
A "p" seems to be missing from "pyplot". You have pylot instead of pyplot in the import statement.
Traceback (most recent call last):
File "try5.py", line 3, in <module>
from matplotlib import pyplot as plt
File "/home/zaverichintan/miniconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 129, in <module>
from matplotlib.externals.six.moves.urllib.request import urlopen
File "/home/zaverichintan/miniconda2/lib/python2.7/site-packages/matplotlib/externals/six.py", line 90, in __get__
result = self._resolve()
File "/home/zaverichintan/miniconda2/lib/python2.7/site-packages/matplotlib/externals/six.py", line 158, in _resolve
module = _import_module(self.mod)
File "/home/zaverichintan/miniconda2/lib/python2.7/site-packages/matplotlib/externals/six.py", line 80, in _import_module
__import__(name)
File "/home/zaverichintan/miniconda2/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/home/zaverichintan/miniconda2/lib/python2.7/httplib.py", line 69, in <module>
from array import array
ImportError: cannot import name array
I have imported numpy , scipy , matplotlib and opencv.
The code getting executed on windows 7 machine but after upgrading to ubuntu 16.04, getting error like this.
the python version shows 2.7 but on the ubuntu site it is showing python 3.
Any help is appreciated.
The problem is likely a configuration issue, because getting the installation correct on Macs seems to be tricky. I'm running Mavericks and matplotlib 1.4.x, yet when I open a Python 2.7.5 shell and import pylib, I get this error:
>>> import pylab
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/pylab.py", line 230, in <module>
import matplotlib.finance
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/finance.py", line 38, in <module>
from matplotlib.collections import LineCollection, PolyCollection
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/backend_bases.py", line 55, in <module>
import matplotlib.textpath as textpath
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/textpath.py", line 22, in <module>
from matplotlib.mathtext import MathTextParser
File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/mathtext.py", line 64, in <module>
import matplotlib._png as _png
ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/_png.so, 2): Library not loaded: /usr/local/lib/libpng15.15.dylib
Referenced from: /Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/_png.so
Reason: image not found
I have libpng16 installed, but not libpng15.
I saw a hardcoded reference to libpng15 in _png.so.
Uninstall and install of matplotlib fixed the issue for me.
You can always moke it by generating a soft link like this
sudo ln -s /opt/X11/lib/libpng.dylib /usr/local/lib/libpng15.15.dylib
Not the cleaner solution but I haven't figure out why matplotlib stubbornly asks specifically for libpng15 and not any 15 or greater.