148

Numpy, scipy, matplotlib, and pylab are common terms among they who use python for scientific computation.

I just learn a bit about pylab, and I got confused. Whenever I want to import numpy, I can always do:

import numpy as np

I just consider, that once I do

from pylab import *

the numpy will be imported as well (with np alias). So basically the second one does more things compared to the first one.

There are few things I want to ask:

  1. Is it right that pylab is just a wrapper for numpy, scipy and matplotlib?
  2. As np is the numpy alias in pylab, what is the scipy and matplotlib alias in pylab? (as far as I know, plt is alias of matplotlib.pyplot, but I don't know the alias for the matplotlib itself)
user
  • 5,370
  • 8
  • 47
  • 75
goFrendiAsgard
  • 4,016
  • 8
  • 38
  • 64
  • 4
    On a generally style note, I would avoid the use of pylab (and * imports) outside of the interactive shell. Pylab is a bit of an odd appendix to matplotlib anyways. – seberg Oct 20 '12 at 11:27
  • @unutbu: thanks for the link, that makes things clear. I think it also means that scipy is an entirely different modules, and thus unrelated to pylab – goFrendiAsgard Oct 20 '12 at 11:58
  • 4
    @goFrendiAsgard: You can inspect exactly what pylab imports by looking in `/usr/lib/pymodules/python2.7/matplotlib/pylab.py` (the exact path is a little different for Windows or OSX; ask if you need help finding it.) – unutbu Oct 20 '12 at 12:12
  • Thank you, that's what I look for. I think I will use the more "pythonic" way since pylab is just a wrapper for they who are use to matlab. – goFrendiAsgard Oct 20 '12 at 12:19
  • 1
    [The mentioned FAQ has a new link.](http://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related) – Robert Pollak Sep 23 '14 at 10:51
  • Closely related to http://stackoverflow.com/questions/16849483/which-is-the-recommended-way-to-plot-matplotlib-or-pylab – tacaswell Jun 07 '15 at 18:59

3 Answers3

136
  1. No, pylab is part of matplotlib (in matplotlib.pylab) and tries to give you a MatLab like environment. matplotlib has a number of dependencies, among them numpy which it imports under the common alias np. scipy is not a dependency of matplotlib.

  2. If you run ipython --pylab an automatic import will put all symbols from matplotlib.pylab into global scope. Like you wrote numpy gets imported under the np alias. Symbols from matplotlib are available under the mpl alias.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
  • 9
    @Benjamin Bannier What is this then - http://wiki.scipy.org/PyLab? This confuses me. – shahensha Mar 30 '15 at 09:28
  • @shahensha , it sounds like there are *two* PyLabs; per the link you sent: "...difference between the vision for a **new PyLab** expressed on this page, and the **existing pylab package which is part of matplotlib**" – Nate Anderson Dec 18 '17 at 01:45
  • [Important update from matplotlib.org:](https://matplotlib.org/3.1.1/tutorials/introductory/usage.html#:~:text=pylab%20is%20a%20convenience%20module,discouraged%20because%20of%20namespace%20pollution.) "`pylab` is a convenience module that bulk imports `matplotlib.pyplot` (for plotting) and `numpy` (for mathematics and working with arrays) in a single namespace. `pylab` is deprecated and its use is strongly discouraged because of namespace pollution. Use `pyplot` instead." – Gabriel Staples May 09 '23 at 05:48
18

Scipy and numpy are scientific projects whose aim is to bring efficient and fast numeric computing to python.

Matplotlib is the name of the python plotting library.

Pyplot is an interactive api for matplotlib, mostly for use in notebooks like jupyter. You generally use it like this: import matplotlib.pyplot as plt.

Pylab is the same thing as pyplot, but with extra features (its use is currently discouraged).

  • pylab = pyplot + numpy

See more information here: Matplotlib, Pylab, Pyplot, etc: What's the difference between these and when to use each?

Felipe
  • 11,557
  • 7
  • 56
  • 103
  • 2
    [Another example posted directly on matplotlib's site](https://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-pylab-how-are-they-related) "Matplotlib, pyplot and pylab: how are they related? `Matplotlib` is the **whole package**; `matplotlib.pyplot` is a module **in matplotlib**; and `pylab` is a module that gets installed **alongside matplotlib.**" – Nate Anderson Dec 18 '17 at 01:53
  • Upvoted. Here's [a more legitimate quote about pylab being discouraged](https://matplotlib.org/3.1.1/tutorials/introductory/usage.html#:~:text=pylab%20is%20a%20convenience%20module,discouraged%20because%20of%20namespace%20pollution.): "`pylab` is a convenience module that bulk imports `matplotlib.pyplot` (for plotting) and `numpy` (for mathematics and working with arrays) in a single namespace. `pylab` is deprecated and its use is strongly discouraged because of namespace pollution. Use `pyplot` instead." – Gabriel Staples May 09 '23 at 05:47
4

Since some people (like me) may still be confused about usage of pylab since examples using pylab are out there on the internet, here is a quote from the official matplotlib FAQ:

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. Although many examples use pylab, it is no longer recommended.

So, TL;DR; is do not use pylab, period. Use pyplot and import numpy separately as needed.

Here is the link for further reading and other useful examples.

james-see
  • 12,210
  • 6
  • 40
  • 47