English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib pylab Module

PyLab is the process interface of Matplotlib's object-oriented plotting library. Matplotlib is the entire package; matplotlib.pyplot is a module in Matplotlib; it and PyLab are modules installed with Matplotlib.

PyLab is a very convenient module that can batch import matplotlib.pyplot (for plotting) and NumPy (for mathematics and using arrays) in a single namespace. Although there are many examples using PyLab, it is no longer recommended to use it.

Basic plotting

Drawing curves is completed using the plot command, which requires a pair of arrays (or sequences) of the same length, as shown in the following example code -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 from numpy import *
 from pylab import *
 x = linspace(-3, 3, 30)
 y = x**2
 #plt.title('title')
 plot(x, y)
 show()

Execute the above code line to generate the following result -

If you want to draw symbols instead of lines, please provide other string parameters. The available symbol parameters are as follows:

Symbols: ^ , v , < , > , s , + , x , D , d , 1 , 2 , 3 , 4 , h , H , p , | , _ , - , –, -, , . , , , o , Color: b, g, r, c, m, y, k, w

Next, let's take a look at the following code -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
from pylab import *
 x = linspace(-3, 3, 30)
 y = x**2
 plot(x, y, 'r|')
 show()

Executing the above example code gives the following result -

You can overlay plots. Just use multiple plotting commands. Use clf() to clear the plot.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 from pylab import *
 x = linspace(-3, 3, 30)
 y = x**2
 plot(x, sin(x))
 plot(x, cos(x), 'r-')
 plot(x, -sin(x), 'g--')
 show()

The following code line generates the following output -