Signup/Sign In

Matplotlib Pylab Module

In this tutorial, we will cover the Pylab module in Matplotlib.

This module mainly helps in bringing functions and classes from pyplot and NumPy into the global namespace which then becomes easier for the Matlab users.

  • This module provides a procedural interface to the Matplotlib object-oriented plotting library.

  • This module gets installed alongside the Matplotlib; Likewise, matplotlib.pyplot is a module in the Matplotlib package.

  • It is a very convenient module as this module helps in importing matplotlib.pyplot (for the plotting) and NumPy (for the mathematics and working with arrays) in bulk in a single namespace.

NOTE: Pylab conflicts with all sorts of built-in functions in Python, so it is abandoned and is not used much.

Syntax to import the Pylab module

The basic syntax to import the pylab module is given below:

from pylab import *

Let us cover basic plotting that uses pylab module:

Example using pylab module:

To plot a curve, in this code example, we will use plot() function. You just need to take same-length arrays in a pair. Let us take a look at the code for the same:

from numpy import *
from pylab import *

x = linspace(-1, 4, 30)
y = x**2
plot(x, y)
show()

Now let us look at the output produced by this:

using pylab module in matplotlib

If there is a need to plot symbols rather than lines then you need to provide an additional string argument in the plot() method.

The possible symbols and colors are as follows:

  • symbols: - , –, -., : , . , , , o , ^ , v , < , > , s , + , x , D , d , 1 , 2 , 3 , 4 , h , H , p , | , _

  • colors: b, g, r, c, m, y, k, w

Let us cover a live example for the same:

Overlaying Plots using pylab

The plots can be overlaid also. For which you need to use multiple plot commands. The clf() function is used to clear the plot.

Let us see a live example for overlaying plots:



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.