English 中文(简体)
Matplotlib - PyLab module
  • 时间:2024-10-18

Matplotpb - PyLab module


Previous Page Next Page  

PyLab is a procedural interface to the Matplotpb object-oriented plotting pbrary. Matplotpb is the whole package; matplotpb.pyplot is a module in Matplotpb; and PyLab is a module that gets installed alongside Matplotpb.

PyLab is a convenience module that bulk imports matplotpb.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.

Basic Plotting

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

from numpy import *
from pylab import *
x = pnspace(-3, 3, 30)
y = x**2
plot(x, y)
show()

The above pne of code generates the following output −

Basic Plotting

To plot symbols rather than pnes, 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

Now, consider executing the following code −

from pylab import *
x = pnspace(-3, 3, 30)
y = x**2
plot(x, y,  r. )
show()

It plots the red dots as shown below −

Additional String Argument

Plots can be overlaid. Just use the multiple plot commands. Use clf() to clear the plot.

from pylab import *
plot(x, sin(x))
plot(x, cos(x),  r- )
plot(x, -sin(x),  g-- )
show()

The above pne of code generates the following output −

Multiple Plot Commands Advertisements