I'm trying to write a program that plots a graph, which made me look into
Matplotlib.
I found a tutorial that started out with this little program, that worked fine:
from pylab import *
def main():
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C,S = np.cos(X), np.sin(X)
plot(X,C)
plot(X,S)
show()
if __name__ == '__main__':
main()
Then I tried to run it on another computer, where it did not work at all. I tried to download Pylab and Matplotlib. When I had installed Matplotlib it demanded something called dateutil, when I got dateutil it demanded something called six. I downloaded six, but it didn't work properly.
It doesn't feel like I'm on the right track. What should I do to get a proper installation?
EDIT:
I'm using Python 2.7 on Windows 7.
The error I get is
Traceback (most recent call last):
File "C:\Users\Python\mscript\listdb2.py", line 19, in <module>
from pylab import *
File "C:\Python27\lib\site-packages\pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python27\lib\site-packages\matplotlib\pylab.py", line 226, in <module>
import matplotlib.finance
File "C:\Python27\lib\site-packages\matplotlib\finance.py", line 21, in <module>
from matplotlib.dates import date2num
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 119, in <module>
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
File "C:\Python27\lib\site-packages\dateutil\rrule.py", line 18, in <module>
from six import advance_iterator, integer_types
ImportError: No module named six
The file six.py is located in C:\python27\Lib\site-packages\six\six.py
The six directory also contains a file called test_six.py. If I try to run this program I also get an error:
Traceback (most recent call last):
File "test_six.py", line 5, in <module>
import.py
ImportError: No module named py
If Anaconda installed and it is already in your environment path, you can get it simply using
conda install matplotlib
in command line and then call in Python with
from pylab import *
This work for me fine as "pip install" and "easy_install" both on Win and Linux caused a lot of issues
Installing packages on *nix is easy using pip. Pip allows you to easily install packages from the Python Package Index (PyPI) with a simple pip install matplotlib. This should install all dependencies, but if it does not then you can install them manually (for instance pip install python-dateutil).
Using pip with Windows is possible though slightly more difficult for packages that require compilers and such. However, installing Python programs on Windows is simple if you use these Windows binaries provided by Christoph Gohlke.
The particular ones for matplotlib can be found here. Note that you can also find similar binaries for python-dateutil, six, etc if necessary.
*As an aside: I would strongly suggest you look into using the full matplotlib.pyplot API rather than pylab. It's much more powerful and useful, but this is just some aside advice :) *
Related
I came across an ImportError in the importing of matplotlib.pyplot on Windows 10 Pro
Here is the full error:
Traceback (most recent call last):
File "C:\Users\****\Dropbox\Code\Python\lib_test.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\pyplot.py", line 31, in <module>
import matplotlib.colorbar
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py", line 16, in <module>
from .path import Path
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\path.py", line 21, in <module>
from . import _path, rcParams
ImportError: cannot import name '_path'
Here is the script:
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()
If anyone could help me, much appreciated.
It can be a problem related to matplotlib version. I had the same problem and solved it uninstalling the existing version of matplotlib (in my case with conda but the command is similar substituing pip to conda) so:
firstly uninstalling with:
conda uninstall matplotlib (or pip uninstall matplotlib)
and after the successful removal of the existing version (mine was 2.2.2), the following command installed a successive version, that can be specified with '==' after the module name:
conda install matplotlib==3.0.2 (or pip install matplotlib==3.0.2)
and the error disappears.
The uninstall command removed also seaborn installation so it was necessary to install it after matplotlib but it was easy...
Yeah!:)
Since you're probably like me and doing a lot of installing/importing, it can be just a mishap in the original matplotlib install... so try doing the ol' get out, walk around to the back side and give it a kick...
conda uninstall matplotlib
#let it do its thing
conda install matplotlib
#let it do its thing
Then test it on the code you're trying to do and see if it works. In my case, it came down to a "_Path" issue in my original install that messed up things. After the uninstall/install, I was fine.
Dunno what mpip is,, or why,, try plain old pip (or in your case perhaps pip3 ? This may have more to do with how the base Python was installed though,, why I suggest using Anaconda to install all (which automatically installs MPL as well..)
I have installed matplotlib-1.5.0.win-amd64-py2.7 from sourcefourge.net after downloading and installing numpy using commandprompt by using pip: pip install numpy. But when I write the following code it Gives me error. Help me out.
Code:
from matplotlib.pylab import *
pylab.plot([1,2,3,4],[1,2,3,4])
pylab.show()
It gives me following error message:
Traceback (most recent call last):
File "C:\Python27\Lib\idlelib\1.py", line 1, in <module>
from matplotlib.pylab import *
File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 124, in <module>
from matplotlib.rcsetup import (defaultParams,
File "C:\Python27\lib\site-packages\matplotlib\rcsetup.py", line 25, in <module>
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
File "C:\Python27\lib\site-packages\matplotlib\fontconfig_pattern.py", line 25, in <module>
from pyparsing import Literal, ZeroOrMore, \
ImportError: No module named pyparsing
There is an easy and complete guide on the Matplotlib Website.
Try and follow this one.
refer to this answer of mine
https://stackoverflow.com/a/38618044/5334188
install numpy
pip install numpy
If you face installation issues for numpy, get the pre-built windows installers from http://www.lfd.uci.edu/~gohlke/pythonlibs/ for your python version (python version is different from windows version).
numpy 32-bit: numpy-1.11.1+mkl-cp27-cp27m-win32.whl
numpy 64-bit: numpy-1.11.1+mkl-cp27-cp27m-win_amd64.whl
Later you require VC++ 9.0, then please get it from below link Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
Then install matplotlib using
pip install matplotlib
If you face errors, please download below from http://www.lfd.uci.edu/~gohlke/pythonlibs/, which will do the rest.
32-bit: matplotlib-1.5.3-cp27-cp27m-win32.whl
64-bit: matplotlib-1.5.3-cp27-cp27m-win_amd64.whl
works like charm
At some point in the last few days, Matplotlib stopped working for me on OS X. Here's the error I get when trying to import matplotlib:
Traceback (most recent call last):
File "/my/path/to/script/my_script.py", line 15, in <module>
import matplotlib.pyplot as plt
File "/Library/Python/2.7/site-packages/matplotlib/pyplot.py", line 34, in <module>
from matplotlib.figure import Figure, figaspect
File "/Library/Python/2.7/site-packages/matplotlib/figure.py", line 40, in <module>
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/Library/Python/2.7/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
from ._subplots import *
File "/Library/Python/2.7/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "/Library/Python/2.7/site-packages/matplotlib/axes/_axes.py", line 22, in <module>
import matplotlib.dates as _ # <-registers a date unit converter
File "/Library/Python/2.7/site-packages/matplotlib/dates.py", line 126, in <module>
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
File "/Library/Python/2.7/site-packages/dateutil/rrule.py", line 14, in <module>
from six.moves import _thread
ImportError: cannot import name _thread
The only system change I can think of was the Apple-forced NTP update and maybe some permission changes I did in /usr/local to get Brew working again.
I tried reinstalling both Matplotlib and Python-dateutil via Pip, but this did not help. Also tried a reboot. I'm running Python 2.7.6, which is located in /usr/bin/python. I'm running Yosemite (OS X 10.10.1).
sudo pip uninstall python-dateutil
sudo pip install python-dateutil==2.2
I had the same error message this afternoon as well, although I did recently upgrade to Yosemite. I'm not totally sure I understand why reverting dateutil to a previous version works for me, but since running the above I'm having no trouble (I generally use pyplot inline in an ipython notebook).
This problem is fixed in the latest six and dateutil versions. However, in OS X, even if you update your six to the latest version, you might not actually update it correctly. This is what happened to me:
After doing a pip2 install six -U, the new six module was installed in /Library/Python/2.7/site-packages/. However, when I loaded six in a python 2.7 terminal, and checked its path, this is what I got:
import six
print six.__file__
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc
So, python was using an old version of six, which I removed by typing:
rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.*
This fixed this issue for me.
Installing the python-dateutil==2.2 did not work for me.
But a quick-and-dirty workaround did work! I replace six.py in python 2.7 with the six.py from python 3.4 (virtualenv). Since, I have the problem in 2.7 but not 3.4.
UPDATE
I had the same problem again after reinstalling python (and after upgrading to El Capitan). Un-obvious thing is that this error occurs only in the IPython shell and notebook (when I do import matplotlib.pyplot as plt) but works fine from a Python shell.
So a better solution (that did work in my case) without a dirty work-around is to force install both six and ipython. Here is what I did to have this fixed :
$ pip install --ignore-installed six
$ pip install --ignore-installed ipython
It is possible that you have a perfectly installed version of any packages you have installed, but the version used by default is not the one you want. You can see the list of paths that python search from in order to find its packages as follows:
>>> import sys
>>> sys.path
In order to let python search first the most updated version of certain package, instead of removing the system version, what can be done is to set the system variable PYTHONPATH in the ~/.bash_profile (or ~/.bashrc if linux) config file to the path where the new packages are installed:
export PYTHONPATH=/Library/Python/2.7/site-packages
An alternative is to modify the python path inside your python script by adding the path at the beginning of the path list:
import sys
sys.path.insert(1,'/Library/Python/2.7/site-packages')
This needs to be done for every script you need a certain package version. You might want for some reason use an older version that you have installed.
BTW all my installations with easy_install, or pip, or from sources go to /Library/Python/2.7/site-packages
This worked en EL Capitan, and now also in macOS Sierra (10.12.2)
As far as I can tell, I don't even need pytz for what I'm doing.
I'm re-learning python for image processing using the O'Reilly book 'Programming Computer Vision with Python' for work (and I'm also new to mac, so on both counts I apologise if this is a stupid question).
Along with a 'empire.jpg' picture, I'm trying to run the script on page 16 which goes as follows:
from PIL import Image
from pylab import *
# read image to array
im = array(Image.open('empire.jpg')) # plot the image
imshow(im)
# some points
x = [100,100,400,400]
y = [200,500,200,500]
# plot the points with red star-markers
plot(x,y,'r*')
# line plot connecting the first two points
plot(x[:2],y[:2])
# add title and show the plot
title('Plotting: "empire.jpg"')
show()
but I get the following output with an error:
File "plotch1.py", line 2, in <module>
from pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/pylab.py", line 208, in <module>
from matplotlib import mpl # pulls in most modules
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/mpl.py", line 4, in <module>
from matplotlib import axes
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 18, in <module>
from matplotlib import dates as mdates
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/dates.py", line 82, in <module>
import pytz
ImportError: No module named pytz
I'm using OS X 10.9.4.
I've installed matplotlib and numpy from macpython.org and I've installed scipy 0.11.0 all for python 2.5.
Do I even need pytz? If not, how can I get around this error?
pylab requires pytz.
The easiest way to install a package in Python is to run pip install pytz.
Today, Python comes with pip pre-installed, but use these instructions if you need to install it: Installation: Do I need to install pip?
Firstly if you have installed pip then remove it by deleting the folder of pip inside python directory. Then install pip by this command:
$ sudo easy_install pip
Then you need to install pytz again by this command:
$ sudo pip install pytz
Don't update pip to 10th version because it might contain bugs which is causing problems.
I am trying to use ryp with my arcpy scripts but I have the following error:
import rpy2.robjects as robjects Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module> import rpy2.robjects as robjects
File "C:\Python26\ArcGIS10.0\lib\site-packages\rpy2\robjects\__init__.py", line 12, in <module> import rpy2.rinterface as rinterface
File "C:\Python26\ArcGIS10.0\lib\site-packages\rpy2\rinterface\__init__.py", line 39, in <module> import win32api
ImportError: No module named win32api
This error comes even after the installation of the pywin32 for my version of python.
I've noticed that this seems to be a common error that is usually solved with the installation of pywin32.
I also have a problem with the matplotlib installation, every time i try to use it (import matplotlib.pyplot as plt), python crashes...
Versions:
Python 2.6.6
matplotlib installation: matplotlib-1.1.0.win32-py2.6.exe
You will need to run these scripts with PROPER Python. It seems to me that the ArcPy distribution does not include the win32api module (It also does not exist from example in Python on Mac or Linux).
I would install PythonXY which includes R bindings, and see if your scripts run there. If they run there, then I (guess) I am correct, and ArcPy does not include these modules.
A nice BONUS of PythonXY is it's an excellent Python IDE (Spyder), but the real bonus is what the commenter above me said:
different compiler versions can cause hell of a lot of Problems.
So, in PythonXY you get a whole bundle compiled with the same compiler.
Let us know if these made your RPy script run.