This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
The original error is, when I try to import matplotlib.pyplot, I got:
Traceback (most recent call last): File "", line 1, in
File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py",
line 114, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py",
line 24, in
from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to
function correctly if Python is not installed as a framework. See the
Python documentation for more information on installing Python as a
framework on Mac OS X. Please either reinstall Python as a framework,
or try one of the other backends. If you are Working with Matplotlib
in a virtual enviroment see 'Working with Matplotlib in Virtual
environments' in the Matplotlib FAQ
I followed the solutions to add a ~/.matplotlib/matplotlibrc file with the code: backend: TkAgg. After doing that, my error changed to:
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.') objc[25120]: Class
TKApplication is implemented in both
/Users/XX/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[25120]: Class TKMenu is
implemented in both /Users/XX/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[25120]: Class
TKContentView is implemented in both
/Users/XX/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[25120]: Class TKWindow
is implemented in both /Users/XX/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.
I have no idea how to fix that. I'm not using a virtual machine.
PS: I found out that by adding:
import matplotlib
matplotlib.use('TkAgg')
before import matplotlib.pyplot, it seems to work. But adding those two lines of code every time is annoying...
What's going on, and how I can fix it?
I run my script in virtualenv. Python version is 3.5.
Add a line:
backend: TkAgg
in file:
~/.matplotlib/matplotlibrc
This solved the problem.
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.
Below worked for me:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference:
https://github.com/tensorflow/tensorflow/issues/2375
I installed Jupyter Notebook in virtualenv and below worked for me:
At Console:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
import matplotlib
Restart the kernel:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
I was using pyenv so the matplotlibrc path wasn't in the home directory, so I created this script to figure the path out and change the backend to Tkagg:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.
Related
Qt5Agg is necessary to use the mayavi 3D visualization package. I have installed PyQt5 and mayavi using pip in a separate copied conda environment. The default backend then changes from TkAgg to QtAgg. This is a bit weird because in an earlier installation in a different PC the default changed directly to Qt5Agg. I always check the backend using the following commands from the python console :
import matplotlib
matplotlib.get_backend()
Even with the backend being 'QtAgg', I am able to use mayavi from the terminal without any issue but not when I do so in Pycharm. Here I get a non-responsive empty window (image below) :
Image of the non-responsive window
I have been able to get rid of this issue by explicitly using Qt5Agg instead of QtAgg before the plt call :
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
But I would prefer a better way than using the above in every script that I write. As I had mentioned earlier, I already have mayavi installed and have used it successfully it in Pycharm in a different PC and there the default backend is 'Qt5Agg' and hence there is no need to change the backend explicitly.
Is there anything obvious that I'm overlooking ? Can you please let me know of a way to change the default backend for matplotlib from QtAgg to Qt5Agg after PyQt5 installation using pip ?
Thanks in advance !!
Thanks to #PaulH's comment, I was able to solve the issue. Owing to #mx0's suggestion, I shall now explicitly mention the fix below so that others can also benefit from it.
In a particular conda environment, if matplotlib package is installed, then there will be a 'matplotlibrc' file stored somewhere that defines what the default backend will be whenever matplotlib is imported from that conda environment. The location of this 'matplotlibrc' can be found using the following commands :
import matplotlib
matplotlib.matplotlib_fname()
Please look into the following link if there's any deprecation issue with the above commands :
https://matplotlib.org/stable/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files
Once the location of the 'matplotlibrc' file is known, open it and simply uncomment one line inside this file. Just change the backend from :
##backend: Agg
to :
backend: Qt5Agg
And that's it. All the plot window troubles in PyCharm will be solved as far as the mayavi 3D visualization package is concerned. For any other use, where a specific backend is necessary, you can also set the default to any other backend of choice.
I tried to use Basemap package to plot a map by PyCharm, but I got something wrong with
from mpl_toolkits.basemap import Basemap`
And the Traceback as followed:
Traceback (most recent call last):
File "/Users/yupeipei/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-0a24a3a77efd>", line 7, in <module>
from mpl_toolkits.basemap import Basemap
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 20, in do_import
module = self._system_import(name, *args, **kwargs)
File "/Users/yupeipei/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 146, in <module>
pyproj_datadir = os.environ['PROJ_LIB']
File "/Users/yupeipei/anaconda3/lib/python3.6/os.py", line 669, in __ getitem__
raise KeyError(key) from None
KeyError: 'PROJ_LIB'
I'm confused with this error on PyCharm, because the same script is running correctly on Jupyter or Spyder! The environment in PyCharm is ../anaconda3/lib/python3.6 where is same from anaconda.
Has anyone met this error before?
Could anyone can help me to solve this error?
For Windows 10 with Anaconda + Python 3.71 (and I'm sure other Python 3 versions and Windows 7/8), you can tell Basemap where Proj4's "epsg" file is to succeed. I don't have an "environment" or whatever because it's too much work to figure out - so I didn't have an anaconda\share\proj area (as far as I could discern why I didn't have it).
But, what Basemap wants is the file "epsg", search the Anaconda directory for it with Windows Explorer. If it doesn't find it, install Proj4 by opening the "Anaconda Prompt" and typing in:
conda install -c conda-forge proj4
If it finds it, it should be in something like:
C:\Utilities\Python\Anaconda\Library\Share (it's where mine was, as well as \pkgs\ places where I guess it puts the package itself - and those can work too if need be, I used them at first, but the library one should work through updates better (maybe)).
Use the following code before importing Basemap and it'll work. Sets the environment variable PROJ_LIB to wherever epsg is and then Basemap can be happy.
import os
os.environ["PROJ_LIB"] = "C:\\Utilities\\Python\\Anaconda\\Library\\share"; #fixr
from mpl_toolkits.basemap import Basemap
As a nice bonus, to get hi-res data of Basemap, which Anaconda doesn't include in the Basemap install to start, type into "Anaconda Prompt":
conda install -c conda-forge basemap-data-hires
Following mewahl's comment I've added to my .bashrc (I use bash):
export PROJ_LIB=/path/to/your/instalation/of/anaconda/share/proj/
and now basemap (and others work).
You have to set the path of Proj lib as in newer version , this path has been replaced.
Write below two lines of code before importing matplot_toolkits
### For Window's Users
import os
os.environ['PROJ_LIB'] = r'C:\Users\XXXXX\Anaconda3\pkgs\proj4-5.2.0-
ha925a31_1\Library\share'
To find the path of Proj_lib , just search epsg and then copy this epsg file location and put in proj_lib .
Your Problem will be solved.
### For Linux's Users
import os
os.environ['PROJ_LIB'] = r'/home/XXXXXX/anaconda3/pkgs/proj4-5.2.0-
he6710b0_1/share/proj'
The answer is from Github and it worked for me.
import os
import conda
conda_file_dir = conda.__file__
conda_dir = conda_file_dir.split('lib')[0]
proj_lib = os.path.join(os.path.join(conda_dir, 'share'), 'proj')
os.environ["PROJ_LIB"] = proj_lib
from mpl_toolkits.basemap import Basemap
This worked for me:
import os
os.environ["PROJ_LIB"] = os.path.join(os.environ["CONDA_PREFIX"], "share", "proj")
This extends #Yusuf Baktir 's answer by omitting hard-coding the path to the epsg file. This way the code works on any machine that has conda installed (and activated of course).
This seems to be a common issue. There are several bug reports about it
basemap/issues/419,
https://github.com/matplotlib/basemap/issues/428
basemap/issues/428
I had run into this error myself, and for me the solution was to uninstall basemap 1.2, then install basemap 1.1 from an old wheel file I still had lying around and then install basemap 1.2 again. I honestly have no idea why that worked though.
Also from those issues above there are all kinds of other solutions that people have reported to work for them. Hopefully one of them fits here as well.
Proj4 easy solution to fix on pycharm is goto setting-> project interpreter -> + -> write proj4 in the search field and install.
I was not able to solve this problem but I was able to find an alternate, use CartoPy.
basemap is being maintained for python 2.7 users.
CartoPy is a better alternative
I faced same problem. I installed anaconda and install conda install -c anaconda basemap.
I used Anaconda built in IDE named "Spyder".
Spyder is better than pycharm. only problem with spyder is lack of intellisense.
I solved problem of Proj4 by setting path.
Other problem kernel restarting when loading of .json larger file dataset.
I use notepad++ and 010 editor to re-save file in small chunks and at last I merged all outputs.
My script needed to import numpy, sklearn, and matplotlib but I couldn't install sklearn. A very helpful response to my question http://https://stackoverflow.com/questions/38733220/difference-between-scikit-learn-and-sklearn explained that I needed to reinstall numpy. Using pip to update numpy failed because OS X 10.11 SIP prevented uninstalling the current numpy. The very helpful answer to a question about pip and SIP by mfripp http://https://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade provided a detailed solution to the problem. I followed those instruction exactly and used pip to reinstall numpy, matplotlib, scipy and sklearn for all users.
When I ran my completed script using the command
python DistMatPlot.py Random10A.matrix Random10A.pdf
the script ran perfectly, writing all expected output files.
However, I always saw:
"/Library/Python/2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.')"
which I had never seen with other matplotlib scripts before updating numpy, matplotlib, etc. The 2 second delay was only mildly annoying.
I compiled a frozen binary using pyinstaller and during the compiling I got several messages similar to that above.
The resulting frozen binary run failed with the command:
./DistMatPlot Random10A.matrix Random10A.pdf
produced the following:
/var/folders/8x/7_zp_33h8xj6td0059b72p9h0000gp/T/_MEIhIysTV/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. Traceback (most recent call last): File
"", line 13, in File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.dict) File "matplotlib/pyplot.py", line 114, in File "matplotlib/backends/init.py", line
32, in pylab_setup File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.dict) File "matplotlib/backends/backend_macosx.py", line 24, in File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 546, in load_module
module = imp.load_module(fullname, fp, filename, ext_tuple) RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are Working with Matplotlib in a virtual enviroment
see 'Working with Matplotlib in Virtual environments' in the
Matplotlib FAQ DistMatPlot returned -1
I have looked at similar questions and tried their suggested solutions to no avail.
(1) Why does matplotlib need to rebuild a font cache each time it is run?
(2) Why does the frozen binary fail when the script itself succeeds? Do I need some additional option other than -F when running pyinstaller?
The issue with the frozen binary turned out to be "RuntimeError: Python is not installed as a framework."
Several posts discussed that issue and suggested adding these two lines before "import matplotlib.pyplot as plt":
import matplotlib
matplotlib.use('TkAgg')
That did not work, but this slight modification did work:
import matplotlib
matplotlib.use('Agg')
I suspect that 'Agg" may be specific to either OS X or to the version of python that is included with OS X 10.11
Pyinstaller can have problems compiling some scripts, particularly if the use either the scipy or matplotlib modules. Compiling appears to succeed, but the compiled script may fail to execute. If the error message indicates that a module, e,g, scipy.stats, could not be found try compiling with the hiddenimport option:
pyinstaller -F --hiddenimport=scipy.stats --hiddenimport=_sysconfigdata GrowthRates.py
When compiling scripts that use scipy or matplotlib Anaconda python helps a lot:
OSX: python27Env
Windows: Use Anaconda python
Linux: Use Anaconda python
I'm trying to put a live mass spectrum plot into a program that I am writing. So far I have a simple GUI that give the program its basic functionality. I also want it to have a live plot of a mass spectrum, a fairly simple 2D bar chart with about 10,000 peaks. I'd like the user to be able to zoom and pan around the plot but the default backend of matplotlib is really slow. I've been reading around and other backends are available and TkApp is recommended. I also want it to be compatible with tkinter Python 3.3.
When I try to change the backend:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy
print(mpl.pyplot.get_backend())
mpl.pyplot.switch_backend('TkAgg')
plot = plt.plot([1, 2, 3, 4, 5, 6])
plt.show(plot)
print(mpl.pyplot.get_backend())
Output:
C:\pyzo2013c\lib\site-packages\matplotlib\mathtext.py:46: UserWarning: Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing has been disabled. Mathtext rendering will be much slower as a result. Install pyparsing 2.0.0 or later to improve performance.
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
Qt4Agg
Traceback (most recent call last):
File "C:\MyPyProgs\Sequencer\spectrum_plot.py", line 7, in <module>
mpl.pyplot.switch_backend('TkAgg')
File "C:\pyzo2013c\lib\site-packages\matplotlib\pyplot.py", line 124, in switch_backend
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\__init__.py", line 28, in pylab_setup
globals(),locals(),[backend_name],0)
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 11, in <module>
import matplotlib.backends.tkagg as tkagg
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\tkagg.py", line 2, in <module>
from matplotlib.backends import _tkagg
ImportError: cannot import name _tkagg
I'm using the Pyzo package. A couple of recommendations as fixes are to install python3-matplotlib-tk however it cannot find pyzo as it's a stand alone installation of python so is not in the registry. Any help would be much appreciated.
Thanks
First, it sounds like the Pyzo package is missing the pyparsing dependency, so that may be one cause of your speed issues.
Second, from the error message it looks like your matplotlib wasn't compiled with Tk support for some reason. You might want to contact Pyzo and see if they're aware of this.
As a workaround, I'd recommend first installing Python from python.org, then visiting Christoph Gohlke's Python Extension Packages for Windows repository. If you haven't already run across it, it's a very large and very up-to-date collection of scientific and other Python modules for Windows, packaged as self-installing .exe files. Many of the packages are linked to NumPy and SciPy compiled with Intel's Math Kernel Library (MKL), which can give significantly better performance in heavy-duty applications. Another nice thing about it is he gives dependency information where needed, so if you visit the matplotlib link it shows what other packages you'll need to download as well.
While you're there, I'd highly recommend checking out IPython, an interactive Python development environment that is just overflowing with features.
I have managed to fix this with the help of Almar from the Pyzo community group. I'm posting it here to help anyone else who might have the same issue.
So the first thing to do is to add pyzo to the registry, this is done by running pyzo_activate.exe. The cmd is then opened, I then answered y to all of the questions.
After this I downloaded the compatible version of matplotlib (and also PyQtGraph as it was recomended) and they could be installed as normal and recognise pyzo as the python version. now when the code from the question is run the following output is produced:
C:\pyzo2013c\lib\site-packages\matplotlib\mathtext.py:46: UserWarning: Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing has been disabled. Mathtext rendering will be much slower as a result. Install pyparsing 2.0.0 or later to improve performance.
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
TkAgg
TkAgg
This means that the default backend of matplotlib is now TkAgg. The pan/zoom controls for plot that is produced are smooth and responsive.
I have been trying to import modules into Ninja IDE for python. These are modules that I have working on the terminal (numpy, scipy, scitools, matplotlib, and mpl_toolkits), but will not run correctly in Ninja.
First I was only getting the message No module named ____. I checked sys.path and found that the path was within the application
/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7 was a typical path. I tried changing the path,but it doesn't seem to do anything to sys.path even after restarting the ide.
But I wanted the path to refer to where the modules are stored (which is /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages). I was able to get numpy and scipy to work as well as parts of mpl_toolkits by adding the contents of my path to the folders that sys.path gave. However, I still can't get fully functioning modules within the ninja ide interpreter. I'll give some examples below of what happens when I import certain modules.
import matplotlib.pyplot
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/matplotlib/__init__.py", line 106, in <module>
ImportError: No module named sysconfig
import mpl_toolkits
from mpl_toolkits.mplot3d import axes3d
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/mpl_toolkits/mplot3d/__init__.py", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 14, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/matplotlib/__init__.py", line 106, in <module>
ImportError: No module named sysconfig
Thanks for the help. I apologize, I am very new to programming, but I did put in about a day and a half of research before posting here.
That's strange as the sysconfig module is a part of Python 2.7 standard library.
Are you sure that Ninja is using the right Python version? Try running:
import sys
print sys.version_info
from Ninja, to see which Python version it is actually using.
I know this question is a few months old, but I wanted to post my solution in case others find it useful. I had a very similar problem, and had a lot of trouble finding a quick workable solution anywhere.
My somewhat roundabout solution was to simply create a virtualenv folder with the version of numpy I wanted, and then pointed the "virtualenv" property for NinjaIDE project to that folder. I restarted NinjaIDE and boom, instantly worked.
To set the virtualenv property for your project via the GUI, go to the Project menu:
Project > Open Project Properties > Project Execution,
and you should see a variable called "Virtualenv Folder". Point that to the folder for your virtualenv, and it should work. (May need to restart NinjaIDE.) This worked for me, NinjaIDE version 2.2 under Ubuntu 12.04.
One quick note: I actually didn't use virtualenv exactly -- I had to use a "conda env," since I am using the Anaconda distribution, and apparently it is not well-tested with virtualenv yet. (I actually got a warning when I went to easy_install virtualenv. Hadn't seen that before.)
Either way, this stackoverflow question has some nice pointers to virtualenv tutorials: Comprehensive beginner's virtualenv tutorial?
Good luck!
I was having a similar problem trying to import a module from /home/paul/lib/python using the console of the Ninja-IDE. I found out that /home/paul/lib/python didn't appear in syspath when checking in the console of the Ninja-IDE. But it did in the terminal!
By starting the Ninja-IDE from the terminal, /home/paul/lib/python was in syspath when checking in the console of the Ninja-IDE. I was now able to import the module I needed.
I hope this might be of some help. If not to ebris1 than maybe to others.