1

Excuse the blunt question, but what's the difference between import pylab and import matplotlib.pyplot? I realized my coworker and I have been using different imports and, to the best of our knowledge, there is no difference between the two imports.

Does anyone choose one over the other for a specific reason? What's the difference?

NoVa
  • 317
  • 3
  • 15

2 Answers2

3

From the documentation:

PyLab is a convenience module that bulk imports matplotlib.pyplot (for plotting) and NumPy (for Mathematics and working with arrays) in a single name space.

So pylab imports more things than the latter. As long as you only use matplotlib.pyplot functions they should be equivalent.

That being said, the docs also mention that it's deprecated:

Although many examples use PyLab, it is no longer recommended

Selcuk
  • 57,004
  • 12
  • 102
  • 110
1

Don't use pylab, it just imports a bunch of junk into the namespace. It was originally intended to provide an environment familiar to people coming from MATLAB.

Instead, use:

import matplotlib.pyplot as plt

Note that pylab's doc itself recommends not to use it:

>>> print(pylab.__doc__)

.. warning::
   Since heavily importing into the global namespace may result in unexpected
   behavior, the use of pylab is strongly discouraged. Use `matplotlib.pyplot`
   instead.

`pylab` is a module that includes `matplotlib.pyplot`, `numpy`, `numpy.fft`,
`numpy.linalg`, `numpy.random`, and some additional functions, all within
a single namespace. Its original purpose was to mimic a MATLAB-like way
of working by importing all functions into the global namespace. This is
considered bad style nowadays.
wim
  • 338,267
  • 99
  • 616
  • 750