Clarification: matplotlib, pyplot, and pylab

I have always been confused about matplotlib, pyplot, and pylab. Actually, I know that matplotlib is a Python library for (scientific) plotting. It provides a lot of plotting functions, from simple graphs to complex ones. But I hadn’t known what pyplot ans pylab were until I read this article.

pyplot is just a wrapper module to provide a Matlab-style interface to matplotlib. Many plotting functions in Matlab are provided by pyplot with the same names and arguments. This will ease the process of moving from Matlab to Python for scientific computation.

pylab is basically a mode in which pyplot and numpy are imported in a single namespace, thus making the Python working environment very similar to Matlab. By importing pylab:

from pylab import *

we can use Matlab-style commands like:

x = arange(0, 10, 0.2)
y = sin(x)
plot(x, y)

iPython shell provides a switch (-pylab) which automatically imports numpy as np and pyplot as plt. The above lines can thus be written as:

x = np.arange(0, 10, 0.2)
y = np.sin(x)
plt.plot(x, y)

Advertisements
This entry was posted in Python and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s