I seem to have a problem that is in parts very similar to the one mentioned here:
Python with eclipse import problem
But unfortunatly just in parts otherwise that would have solved mine as well.
I use Eclipse SDK, Version: 3.7.0 with PyDev 101.
Furthermore I have installed
numpy-1.6.1rc1-win32-superpack-python2.6.exe
and
matplotlib-1.0.1.win32-py2.6.exe
as noted here:
http://matplotlib.sourceforge.net/users/installing.html
I have rebuild all the packages and looks the site-packages are listed.
(by the way as you see it is an Python version installed with ArcGIS )
If I test a script for instance a very simple one like:
import numpy
import matplotlib
import pylab as pl
I get the following error in Eclipse:
import matplotlib
import pylab as pl
from matplotlib.pylab import *
ImportError: No module named pylab
Even though the interpreter for Pydev is pointing to the appropriate version of python and matplotlib is installed properly in there (site-packages) it does not work in Eclipse. In iPython it works perfect.
What still needs to be done to get matplotlib work in Eclipse?
Thanks a lot!
Werner
pylab is in matplotlibs namespace, so this should work:
import matplotlib.pylab as pylab
I found that turning off interactive move and then calling show worked.
import matplotlib.pyplot as plt
#...your code...
plt.ioff()
plt.show()
Related
Within Jupyter notebook I can import matplotlib but not the pyplot module:
import matplotlib % works
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
An alternative test also fails:
import matplotlib
matplotlib.pyplot % AttributeError: module 'matplotlib' has no attribute 'pyplot'
However, I can import the pyplot module from the conda command prompt with no errors:
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
I got the same results in both the "base" environment and a virtual environment that I created.
Does anyone know what the problem is? I've tried uninstalling and reinstalling the maplotlib package, as well as Jupyter notebook, and conda itself.
This imports matplotlib module/library and then you are trying looks for an attribute or variable defined in matplotlib library named as pyplot; which does not exist. pyplot is just an interface for you to call other relevant interactive state-based functions to plot.
import matplotlib
matplotlib.pyplot
In my opinion you should stick to naming/importing convention defined in documentations of libraries for coherent code.
import matplotlib.pyplot as plt
This is all you need.
import matplotlib.pyplot as plt % The kernel appears to have died. It will restart automatically.
This means you have to restart your juypter there will be a cmd window that you could have closed I recommend you to restart the juypter and then
import matplotlib.pyplot as plt
if it does not work
open juypter cmd from search bar and write pip install matplotlib after the task is done in jupyter cmd reopen juypter and than use
import matplotlib.pyplot as plt
Not that I understand what the problem was, but what worked for me is uninstalling matplotlib with conda, and re-installing it with pip. Oftentimes, the conda versions are not the latest, and there is probably a problem with the specific version. As of matplotlib 3.6.1 pyplot does work in jupyter (I tried with base), though reinstallation of kiwisolver might be needed as well.
from skimage import io
photo=io.imread('myimage.jpg')
print(type(photo))
photo.shape
print(photo.shape)
import matplotlib.pyplot as plt
plt.imshow(photo)
This is my code on my editor. I don't use anacona distribution or jupyter notebook. print(type(photo)) and print(photo.shape) lines works but plt.imshow(photo) doesn't.How to fix it? Though ,I have installed those necessary modules and packeges.
My second question , is there another python distribution which is so efficient like anaconda.
I'm still fairly new to python and am wondering if the x.y statement means y is a submodule of x? And if so, doesn't the command:
import matplotlib.pyplot as plt
only import this particular submodule and nothing else? I had to do this in order to get access to the hist function. How does that affect the modules normally imported when calling import matplotlib as plt? Can I get all the modules in matplotlib together under the plt name?
I'm aware that this question is related to what is the difference between importing python sub-modules from NumPy, matplotlib packages But the answer in this question does not tell me if nothing else in matplotlib is imported and how to just import all of matplotlib without worrying about submodules being left out.
Have a look at this codebase tree: matplotlib contains a library of code, while pyplot is only a file of this lib.
import matplotlib
will imports all the files inside this repo. For example to use it:
import matplotlib as mpl
mpl.pyplot.plot(...)
To import pyplot:
from matplotlib import pyplot as plt
# or
import matplotlib.pyplot as plt
plt.plot(...)
One question for you: what console do you use? I guess it's Ipython console or something?
Edit:
To import all:
from matplotlib import *
pyplot(...)
Why do I guess you are using Ipython? Ipython console imports all modules from numpy and some other libraries by default on launch, so that in Ipython console you can simple use: sqrt, instead of import math; math.sqrt, etc. matplotlib is imported in Ipython be default.
I don't know of any way to import all the functions from every submodule. Importing all the functions from a submodule is possible the way you suggested with e.g. from matplotlib.pyplot import *.
Be noted of a potential problem with importing every function; you may override imported functions by defining your own functions with the same name. E.g:
from matplotlib.pyplot import *
def plot():
print "Hello!"
plot()
would output
Hello!
I had conda installed, which has added stuff to ~/.bashrc.
Commenting that made it work for me.
I am trying to set a style in matplotlib as per tutorial http://matplotlib.org/users/style_sheets.html
import matplotlib.pyplot as plt
plt.style.use('ggplot')
but what I get in return is:
AttributeError: 'module' object has no attribute 'style'
My matplotlib version is 1.1.1 (and I'm on a Mac running Mavericks). Where are the styles in this version?
thanks!
My matplotlib version is 1.1.1
There's your problem. The style package was added in version 1.4. You should update your version.
In ipython notebook I also had to include %matplotlib inline, otherwise I would still get the same error.
%matplotlib inline
import matplotlib
matplotlib.style.use('ggplot')
For people using matplotlib 2.x and discovering this question can use the following snippet:
import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic') # any style.
This is described in the documentation here. Note the import matplotlib.style is important.
I tried all solutions listed on StackOverflow but somehow none of these work for me. Finally I found a method which worked. Following are the details:
Environment:
OS : Ubuntu 16
Python Version : 3.5.
MatPlotLib Version : 2.0.2
Correct Way of importing 'style module'
import matplotlib
matplotlib.use
import matplotlib.pyplot as plt
plt.style.use('ggplot')
The matplotlib help reads:
:func:~matplotlib.use (ignore syntax as "`" did not work either on command line or script file)
a function for setting the matplotlib backend. If used, this
function must be called immediately after importing matplotlib
for the first time. In particular, it must be called
before importing pylab (if pylab is imported).
Somehow without issuing this command, it was not possible to access the 'Style' module.
Hope this helps.
For MatPlotLib version 3.6.3 and above, the following code to be used to use "seaborn" style as the seaborn has been deprecated since version 3.6:
import matplotlib
matplotlib.use
import matplotlib.pyplot as plt
plt.style.use("seaborn-v0_8")
I'm new to Python and matplotlib. A simple script I wrote is crashing and I was able to reproduce the crash with the following code:
import matplotlib.pyplot as plt
plt.figure(1)
plt.figure(2)
#plt.show()
The error is python.exe has stopped working. If I uncomment the plt.show(), it still crashes depending on the order I close the plots (no crash if 2 is closed first, crash if 1 is closed first). I'm using Windows 7, Python 3.4, and I installed the individual modules from www.lfd.uci.edu/~gohlke/pythonlibs/. Do I have something configured incorrectly or a misunderstanding of how to use matplotlib?
You need to set the TkAgg backend explicitly. With the following code, the problem is resolved.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
Note that setting the TkAgg backend after importing pyplot does not work either; it crashes too. You need to set it before importing pyplot.
I was having this issue, I thought it was some line in my code causing the bug, but in fact the very act of importing matplotlib.pyplot was killing my program. I solved it by first running it in verbose mode:
python -v [programname].py
This shows the last action the importer does before crashing. For me, the last line of this was:
import 'PyQt5' # <_frozen_importlib_external.SourceFileLoader object at 0x000001F8EC9C0908>
This suggested to me that the dependent library PyQt5 was causing issues, so I ran pip install PyQt5, and magically everything started working.
This could be issue with python 3.x
I have tried with python 2.7 on my windows machine and it works perfectly fine!
You can either downgrade your python to 2.7 or if you feel its too late to do why dont you give it a try to call close()
Import matplotlib
matplotlib.use('wxAgg')
Import matplotlib.pyplot as plt
# your scripts
plt.close('all')
I had a similar issue in OSX when I updated to Python 3.4. IDLE was also crashing and there was a warning telling me the version was unstable.
I solved it by following the prompts and updating the version of Tcl/Tk (8.5.9) - http://www.python.org/download/mac/tcltk .
For macOS, just make sure that
~/.matplotlib/matplotlibrc contains:
backend: MacOSX
You don't need the other backends unless you specifically want them. Alternatively, perhaps you can do:
import matplotlib
matplotlib.use("MacOSX")
though I have not tested that.