I usually use the following package to create my plots: matplotlib.pylab
. However, there is also a package called matplotlib.pyplot
.
I have not been able to spot any difference between the two when using them. So my question is the following:
What is the difference between the packages matplotlib.pylab
and matplotlib.pyplot
. In which cases would you advice one over the other?
pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib. 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.
Pyplot is an API (Application Programming Interface) for Python's matplotlib that effectively makes matplotlib a viable open source alternative to MATLAB. Matplotlib is a library for data visualization, typically in the form of plots, graphs and charts.
Since heavily importing into the global namespace may result in unexpected behavior, the use of pylab is strongly discouraged.
Pylab is a module that provides a Matlab like namespace by importing functions from the modules Numpy and Matplotlib. Numpy provides efficient numerical vector calculations based on underlying Fortran and C binary libraries. Matplotlib contains functions to create visualizations of data.
Per the FAQ:
Pyplot provides the state-machine interface to the underlying plotting library in matplotlib. This means that figures and axes are implicitly and automatically created to achieve the desired plot....
Pylab combines the pyplot functionality (for plotting) with the numpy functionality (for mathematics and for working with arrays) in a single namespace, making that namespace (or environment) even more MATLAB-like. For example, one can call the sin and cos functions just like you could in MATLAB, as well as having all the features of pyplot.
The pyplot interface is generally preferred for non-interactive plotting (i.e., scripting). The pylab interface is convenient for interactive calculations and plotting, as it minimizes typing. (my emphasis.)
Note that
from pylab import *
also performs
from numpy import *
This overwrites many builtin Python functions such as:
In [5]: import __builtin__
In [6]: import numpy as np
In [5]: {name for name in set(dir(np)).intersection(dir(__builtin__)) if not name.startswith('__') and getattr(__builtin__, name) != getattr(np, name)}
Out[5]: {'abs', 'all', 'any', 'max', 'min', 'round', 'sum'}
Therefore, I don't like from pylab import *
(or really from module import *
for any module) because it makes well-known beloved Python names behave in unexpected ways (if you do not always keep in mind that from numpy import *
has polluted the global namespace.)
For example,
In [32]: np.all([np.arange(3), np.arange(3)])
Out[32]: False
while
In [33]: all([np.arange(3), np.arange(3)])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With