matplotlib basic question - python

Please forgive my simple question. I have just started to use Matplotlib and I am having some difficulty.
I can run the following with the interpretor without problems:
>>> from pylab import *
>>> plot([1,2,3])
>>> show()
The above code generates a beautiful graph.
However, if I place the following code inside a file and run it, I get an error:
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
from pylab import *
plot([1,2,3])
show()
Here is the error message:
Traceback (most recent call last):
File "/Users/sbrown/Desktop/new1.py", line 12, in <module>
from pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 133, in <module>
from matplotlib.rcsetup import (defaultParams,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/colors.py", line 54, in <module>
import matplotlib.cbook as cbook
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook.py", line 15, in <module>
import new
File "/Users/sbrown/Desktop/new.py", line 2, in <module>
plot([1,2,3])
NameError: name 'plot' is not defined
>>>
Any idea what the problem could be? Thanks in advance for any help you can provide!

Looks like you have a file on your Desktop that is shadowing the standard Python new module:
>>> import new
>>> new
<module 'new' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/new.pyc'>
Rename or remove $HOME/Desktop/new.py and try again.

Related

Attribute error whenever I import matplotlib

Everytime I import matplotlib into my python programs I get this error (here the program is just one line "import matplotlib.pyplot as plt"):
Traceback (most recent call last):
File "string.py", line 1, in \<module>
import matplotlib.pyplot as plt
File "D:\Other programs\Python38\lib\site-packages\matplotlib\__init__.py", line 94,
in \<module>
import logging
File "D:\Other programs\Python38\lib\logging\__init__.py", line 28, in \<module>
from string import Template
File "D:\programs\python\string.py", line 1, in \<module>
import matplotlib.pyplot as plt
File "D:\Other programs\Python38\lib\site-packages\matplotlib\pyplot.py", line 36, in \<module>
import matplotlib.colorbar
File "D:\Other programs\Python38\lib\site-packages\matplotlib\colorbar.py", line 40, in \<module>
import matplotlib.artist as martist
File "D:\Other programs\Python38\lib\site-packages\matplotlib\artist.py", line 17, in \<module>
_log = logging.getLogger(__name__)
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely
due to a circular import)
I cannot find any logging.py anywhere. Is this some sort of bug in the lates version of matplotlib?
your script D:\programs\python\string.py, where you try to import matplotlib.pyplot as plt is named string.py and it interferes with string module from standard library. Rename that file.

"Import Error: cannot import name 'unicode_literals' "

Sorry if this is a dumb question but I'm trying to import and open a CSV using pandas in Python. Whenever I hit run I get the syntax error "cannot import name 'unicode_literals'". I have no idea why that is happening and I haven't been able to find any source online which details what this error means.
This is my code:
import pandas as pd
with open(r"FILEPATH\File.csv") as rawData:
pd.read_csv(rawData)
Here is the Error:
C:\Anaconda3\python.exe "FILEPATH"
Traceback (most recent call last):
File "FILEPATH/Main.py", line 1, in <module>
import pandas as pd
File "C:\Anaconda3\lib\site-packages\pandas\__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas\src\numpy.pxd", line 157, in init pandas.hashtable (pandas\hashtable.c:22997)
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 107, in <module>
from __future__ import division, absolute_import, print_function
File "C:\Anaconda3\lib\__future__.py", line 23, in <module>
from __future__ import unicode_literals
ImportError: cannot import name 'unicode_literals'
cannot import name 'unicode_literals'
Any suggestions for why this isn't working would be greatly appreciated.
You're on the right track! The only thing you have to do is add another parameter to open(). This would yield:
import pandas as pd
with open(r"FILEPATH\File.csv", encoding='utf-8') as rawData:
pd.read_csv(rawData)

I am having trouble using or importing matplotlib. This is the first line of my code: import matplotlib.pyplot as plt

These are the messages I'm receiving and I have no idea what any of them mean.
I have python 2.7.12 and have imported matplotlib.
In the command prompt window, the last update I installed was conda install matplotlib which seemed to update some stuff (conda-env-2.5.2 and conda-4.1.12), but unfortunately not the stuff I needed.
I have never used any of this before so please speak in layman's terms if possible.
Traceback (most recent call last):
File "C:\Users\user\Desktop\plotting2.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 36, in <module>
from matplotlib.figure import Figure, figaspect
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 40, in <module>
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "C:\Python27\lib\site-packages\matplotlib\axes\__init__.py", line 4, in <module>
from ._subplots import *
File "C:\Python27\lib\site-packages\matplotlib\axes\_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 23, in <module>
import matplotlib.dates as _ # <-registers a date unit converter
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 130, in <module>
import dateutil.parser
File "C:\Python27\lib\site-packages\dateutil\parser.py", line 38, in <module>
from calendar import monthrange, isleap
ImportError: cannot import name monthrange
It looks like there may be a conflicting module named calendar. Are there any other packages (files) called "calendar" (calendar.py) in your python path? Try opening up a new python console and type from calendar import monthrange.

module importing error exclusively when running a file

I'm trying to import and use matplotlib.pyplot in a script and I'm getting the following error:
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
from matplotlib import pyplot as plt
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/pyplot.py", line 36, in <module>
from matplotlib.figure import Figure, figaspect
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/figure.py", line 40, in <module>
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
from ._subplots import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/axes/_axes.py", line 22, in <module>
import matplotlib.dates as _ # <-registers a date unit converter
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/matplotlib/dates.py", line 126, in <module>
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/dateutil/rrule.py", line 13, in <module>
from fractions import gcd
ImportError: cannot import name 'gcd'
The weird part is that I can import it just fine if I restart the Python shell and import it directly, but as soon as I try to run my script, even if I run the script and then import it after running the script to generate data, I get the error. The import line, if it matters, is always the same:
import matplotlib
from matplotlib import pyplot
My script is being run from a folder on my desktop, and I installed matplotlib in terminal with pip3.
Your file called fractions.py is shadowing the builtin module of the same name, causing problems when other libraries try to use that library. Name your file something else.

importing NumPy in Parallel Python

everyone,I am new to PP but got stuck in a problem when importing NumPy with PP.
Basically what I tried to do was submitting a function to the ppserver which depends on NumPy. I have imported it at the very beginning of the code using (import NumPy as nu) but when I ran the code, it gave me the error that cannot find the shared object multiarray.so.
The situation is exactly the same here: parallel python forum
the code is attached as below: (I am running on python 2.7.2 + pp 1.6.0 + numpy 1.5.1)
import numpy as nu
import pylab as pl
import pp
job_server = pp.Server(secret="123456")
print "Starting pp with", job_server.get_ncpus(), "workers"
aa = GrRib()
job = job_server.submit(aa.plotwavefunc, (band,k),(nu,pl,signal))
result = job()
the error looks like :
An error has occured during the function import
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/ppworker.py", line 86, in run
exec __fobj
File "<string>", line 127, in <module>
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python intepreter from there.
An error has occured during the function import
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/ppworker.py", line 86, in run
exec __fobj
File "<string>", line 1, in <module>
File "/usr/share/pyshared/matplotlib/__init__.py", line 135, in <module>
from matplotlib.rcsetup import (defaultParams,
File "/usr/share/pyshared/matplotlib/rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "/usr/share/pyshared/matplotlib/colors.py", line 52, in <module>
import numpy as np
File "/usr/share/pyshared/numpy/__init__.py", line 136, in <module>
import add_newdocs
File "/usr/share/pyshared/numpy/add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "/usr/share/pyshared/numpy/lib/__init__.py", line 4, in <module>
from type_check import *
File "/usr/share/pyshared/numpy/lib/type_check.py", line 8, in <module>
import numpy.core.numeric as _nx
File "/usr/share/pyshared/numpy/core/__init__.py", line 5, in <module>
import multiarray
ImportError: No module named multiarray
Can anyone help me out? I understand it as I will have to change the directory PP is looking for pyshared objects.
I could be wrong, but I believe the way you submit the job is wrong. As seen on the documentation (http://www.parallelpython.com/content/view/15/30/#QUICKSMP), the third argument (not including "self") are dependent functions. Also, the modules have to be strings. I'm assuming the "band" and "k" are dependent functions since I do not see their declarations:
job = job_server.submit(aa.plotwavefunc, depfuncs = (band,k), modules = ("numpy","pylab","signal"))

Categories