12

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.

Community
  • 1
  • 1
Moppentapper
  • 299
  • 2
  • 4
  • 10
  • What do you mean you can't use the `plot` function anymore? – Plasma Apr 16 '16 at 10:11
  • @Plasma After importing matplotlip.pyplot you can't use plt.plot(x,y) anymore (module not imported...?) or did I simply do something dumb? – Moppentapper Apr 16 '16 at 10:12
  • So if you say `import matplotlib.pyplot as plt` `plt.plot()` won't work? Or do you say `import matplotlib.pyplot` and then `plt.plot()`? – Plasma Apr 16 '16 at 10:17
  • @Plasma you are right, I just tried this, must have done something wrong before. I modified the question but it confuses me even more... Is only the pyplot module imported? Or all of matplotlib including the submodule pyplot? – Moppentapper Apr 16 '16 at 10:53
  • 2
    pyplot is a submodule of matplotlib, it might be clearer if you look at it this way of importing it: `from matplotlib import pyplot as plt`. You could say `from matplotlib import *`, and you'd import [every module](http://matplotlib.org/py-modindex.html) in matplotlib, including pyplot (and plot by calling `matplotlib.pyplot.plot`. However, if you're going to plot stuff, `pyplot` is probably the only one you'll need. – Plasma Apr 16 '16 at 11:16
  • @Plasma That now makes a lot more sense to me! thanks! So last question: Is there a way to import all the functions contained in all the submodules of matplotlib right into the workspace? So something similar to `from matplotlib import *` but that imports all the functions in the submodules so that you can simply use `plot(x,y)`? Or would you have to do this for every submodule separately (`from matplotlib.pyplot import *` and similarly for all other submodules)? – Moppentapper Apr 16 '16 at 13:49
  • I don't know of any way to import all the functions from every submodule. Importing all the functions from every submodule is possible the way you suggested with e.g. `from matplotlib.pyplot import *`. – Plasma Apr 16 '16 at 13:54
  • @Plasma Thanks for your help! I understand how this works now, and I guess I'll just have to get used to these libraries and modules! Bit difficult to wrap your head around initially. So thanks! – Moppentapper Apr 16 '16 at 13:56
  • you're very welcome. I added an answer which includes a potential risk when importing with *. I'd recommend importing explicitly, as it's easier to understand what is going on. – Plasma Apr 16 '16 at 14:00

3 Answers3

11

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.

knh170
  • 2,960
  • 1
  • 11
  • 17
  • Yes, I'm using an IPython console inside Spyder. And thanks, that makes sense. I'm still wondering (and I just asked @Plasma the same question above) if there's a way to import all the functions in all the submodules then directly? This may not be something desirable, but it seems to me like it would be easier to just import everything that matplotlib contains and use simple statements such as `plot(x,y)` at least for the first few months of learning how to use python properly – Moppentapper Apr 16 '16 at 13:53
  • Thanks! I didn't know IPython imported some of those libraries automatically, but it explains a few things I noticed when using it! Your edit works in a way that now I could use pyplot.plot(x,y) but I couldn't just type in plot(x,y). That's ok though, thanks to you and Plasma I understand what's going on now! – Moppentapper Apr 18 '16 at 00:16
  • @Moppentapper Are you really understanding? `plot` is a function in `matplotlib.pyplot` when only imported `matplotlib.*` imported and you complain can't simply `plot`?? Upvote an answer when it's useful. – knh170 Apr 18 '16 at 05:23
  • the way I understand it is this: pyplot is a submodule of matplotlib. writing `from matplotlib import *` imports all the submodules into the workspace. In this case, to use the `plot` function, instead of writing `matplotlib.pyplot.plot(x,y)` I can write `pyplot.plot(x,y)` but I cannot write `plot(x,y)` as `plot` won't be defined in the workspace directly. If `plot` is to be used without a "prefix" then I have to use `from matplotlib.pyplot import *` but I cannot import all the functions from all the submodules into the workspace directly. Is this correct? – Moppentapper Apr 18 '16 at 05:28
  • 1
    Python is a lot more fun when you understand what's going on!! – Moppentapper Apr 18 '16 at 13:22
  • Turns out that `import matplotlib as mpl; mpl.pyplot.plot(3)` does **not** work: `AttributeError: module 'matplotlib' has no attribute 'pyplot'`. This is because importing a package (i.e., directory with `__init__.py`) does not automatically import its modules (`.py` files in that directory). – ijoseph Sep 24 '20 at 04:03
2

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!
Plasma
  • 1,903
  • 1
  • 22
  • 37
  • That makes sense, so I assume that at least one function is defined in at least 2 submodules of matplotlib. Then importing it all would cause shadowing. Interesting stuff now this is all built up! – Moppentapper Apr 16 '16 at 14:04
0

I had conda installed, which has added stuff to ~/.bashrc.

Commenting that made it work for me.

user1953366
  • 1,341
  • 2
  • 17
  • 27