Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between importing matplotlib and matplotlib.pyplot?

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.

like image 302
Moppentapper Avatar asked Apr 16 '16 08:04

Moppentapper


1 Answers

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.

like image 192
knh170 Avatar answered Oct 03 '22 14:10

knh170