Skip to content

Matplotlib - PyLab module

You might find older Matplotlib examples or tutorials using pylab. PyLab is a module that gets installed alongside Matplotlib and was created as a convenience layer, especially for users transitioning from MATLAB.

What PyLab Does: It bulk imports matplotlib.pyplot (usually as plt) and numpy (usually as np) into the main namespace. This means you could call functions like plot() or array() directly without the plt. or np. prefix.

# --- Old PyLab Style (Discouraged) ---
# from pylab import *
# x = linspace(-3, 3, 30) # linspace is from numpy, imported by pylab
# y = x**2
# plot(x, y) # plot is from pyplot, imported by pylab
# title('My Plot')
# show()

While seemingly convenient, using pylab (especially via from pylab import *) is strongly discouraged in modern Python code for several reasons:

  • Namespace Pollution: Importing everything (*) floods your current namespace with numerous functions and objects from both pyplot and numpy. This makes it hard to track where a specific function came from and increases the risk of naming conflicts with your own variables or functions, or functions from other libraries.
  • Readability and Maintainability: Code becomes less clear because it’s not immediately obvious whether plot refers to matplotlib.pyplot.plot or potentially something else. Explicit imports (import matplotlib.pyplot as plt, import numpy as np) make the origin of functions explicit (plt.plot(), np.array()), significantly improving code readability and making it easier to understand and maintain.
  • Implicit State: Like pyplot, pylab relies on implicit state management (operating on the ‘current’ figure/axes), which can become confusing in larger scripts or applications. The object-oriented approach is generally preferred for better control.
  • Against Python Best Practices: Importing * is generally considered bad practice in Python, except in specific controlled situations (like internal module organization). Explicit imports are the standard.

The standard and recommended way to use Matplotlib and NumPy is through explicit imports using conventional aliases:

# --- Modern Recommended Style ---
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 30) # Explicit np.linspace
y = x**2
fig, ax = plt.subplots() # Use object-oriented interface
ax.plot(x, y) # Explicit ax.plot
ax.set_title('My Plot') # Explicit ax.set_title
plt.show() # Explicit plt.show

Or, for simple interactive use where the pyplot interface is sufficient:

# --- Simple Pyplot Style ---
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 30) # Explicit np.linspace
y = x**2
plt.plot(x, y) # Explicit plt.plot
plt.title('My Plot') # Explicit plt.title
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show() # Explicit plt.show

Conclusion: Avoid using pylab. Stick to explicit imports (import matplotlib.pyplot as plt, import numpy as np) and prefer the object-oriented API (fig, ax = plt.subplots()) for clarity, control, and maintainability in your Matplotlib code.