What is pylab?


In this article, we will show you the differences between seaborn and matplotlib libraries for data visualization in python.

MATLAB is often regarded as the greatest tool for creating graphs and charts, but not everyone has access to it. In Python, there are several interactive modules that allow us to plot graphs and charts in the output, but we will focus on the module that gives us with a MATLAB-like namespace by importing functions.

Python PyLab Module

PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib.pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib.

Python PyLab Module

PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib.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.

Installation

The PyLab Module is installed at the same time as the Matplotlib package. However, if we wish to use this module in a Python program, we must first ensure that the Matplotlib Module is installed on our system. If Matplotlib is not already installed on the system, we may use the pip installer command in the command prompt terminal shell to install Matplotlib Module and so get the PyLab Module

pip install matplotlib

Basic Plotting

Plotting curves is done with the plot() function. It takes a pair of same-length arrays (or sequences) −

Algorithm (Steps)

Plotting curves is done with the plot() function. It takes a pair of same-length arrays (or sequences) −

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import all the functions(represented by *) from the numpy, pylab modules.

  • Use the numpy.linspace() function(returns number spaces evenly with respect to the interval) to generate random points in x-axis

  • Get the y-axix values as the square of the x-axis values.

  • Plot the x,y values using the plot() function.

  • Display the plot using the show() function.

Example

The following program returns a basic plot using plot() function of pylab module −

# importing all functions from numpy module from numpy import * # importing all functions from pylab module from pylab import * # x-axis of the curve x = linspace(-3, 3, 30) # y-axis of the curve y = x**2 # plotting the curve of x and y axis plot(x, y) # displaying the plot show()

Output

On executing, the above program will generate the following output −


Advanced Plotting

We can utilize some variables in the plot() function, in addition to the x and y variable arguments, to plot more interactive curves with the PyLab Module. To print symbol lines instead of straight lines in curves, we must pass additional string arguments to the plot() function.

Aside from that, we can print the lines in colors other than the default color plotted in the output curve, and we must follow the same set of instructions to do so. The color argument must be passed as an additional argument to the plot() function in order for the line of curve displayed in the output to be printed in the color of our choice.

To plot symbols rather than lines, provide an additional string argument.

symbols - , –, -., , . , , , o , ^ , v , < , > , s , + , x , D , d , 1 , 2 , 3 , 4 , h , H , p , | , _
colors b, g, r, c, m, y, k, w
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)

Parameters

start(optional) − It is the start values of an interval range. Default is 0.

stop − It is the end value of an interval range.

num(optional) − numbers of samples to generate(int)

retstep − If True, then return (samples, step). Restep is set to False by default.

dtype − It is the type of result array

Example

The following program returns a advanced plot using plot() function of pylab module with some added style to the plot like color, type of plot −

# importing all functions from numpy module from numpy import * # importing all functions from pylab module from pylab import * # x-axis values of the curve x = linspace(-3, 3, 30) # y-axis of the curve y = x**2 # plotting the curve of x and y axis in red color with dotted lines(.) plot(x, y, 'r.') # displaying the plot show()

Output

On executing, the above program will generate the following output −


Example 2

The following program returns the multiple plots using the pylab module −

# importing all functions from numpy module from numpy import * # importing all functions from pylab module from pylab import * # Multiple variables defined for multiple plot() functions a = linspace(7, 8, 9, 10) b = linspace(2, 3, 4, 5) c = linspace(3, 5, 7, 9) x = a ** 2 y = b ** 2 z = c ** 2 # Using the plot() function several times plot(a, x, 'y-') plot(b, y, 'c--') plot(c, z, 'g.') # displaying the plot show()

Output

On executing, the above program will generate the following output −


As seen in the output, we plotted multiple curves and overlaid them by calling the plot() function multiple times in the programme.

Conclusion

In this tutorial, we learned about PyLab Module, which provides us with a MATLAB-like namespace and is very useful for plotting multipurpose curves using a Python programme. By importing functions from PyLab and Numpy Module into the programme, we were able to plot various types of curves in the output. Following our introduction to basic plotting, we plotted curves with a symbol line or/and colour in line, and we overlaid curves.

Updated on: 20-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements