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.
Related
I need to set the backend for matplotlib in order to apply this solution for addressing issues with non-thread-safe code in Tkinter and using the Tk backend in matplotlib. According to the post I need to do
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
but setting the backend must be done before any imports of pyplot. I thought I found the first import, but I keep getting this warning:
/usr/lib/python2.7/dist-packages/matplotlib/__init__.py:1352: UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.
My codebase is somewhat large and complex, so it's becoming a pain for me to try to track down where the very first import of pyplot is. Is there a way to test this programmatically, or is there a debug tool I can use to figure this out?
Try matplotlib.use('Agg',warn=False,force=True). This might be duplicate of How to switch backends in matplotlib / Python
Somewhere in my library code I have a matplotlib.pyplot import. When I import one of my library modules I see that pyplot ends up getting imported (as evidenced by the fact that, in ipython notebook, my notebook immediately crashes because I did not run %matplotlib inline).
How can I get a stacktrace showing the sequence of imports (starting with my library module import) that resulted in pyplot getting imported? So far I have tried using sed to replace all my instances of
from matplotlib import pyplot as plt
to
import pdb; pdb.set_trace(); from matplotlib import pyplot as plt
but that hasn't turned it up yet.
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")
So I'm trying to write a piece of code that creates a figure, but in order for it to run on the computer that I want it to (school one), I can't use the x-window backend to create the figure. I tried switching the backend use (I have the matplotlib.use('Agg') statement in my code), but whenever it creates the figure, it gives me 3 colorbars on the figure when I just want one. It also gives me the error
UserWarning: This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
warnings.warn(_use_error_msg)
My code starts like this:
import matplotlib
matplotlib.use('Agg')
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
Any ideas or help?
Late reply, but I wanted to document this for anyone else.
You're likely using Spyder as your python interface. When you start Spyder it automatically loads those modules, hence the error about how it's already been chosen. There's a link here that explains it: https://groups.google.com/forum/#!topic/spyderlib/tRwgqEAIyvs
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()