Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the recommended way to plot: matplotlib or pylab?

I can plot in Python using either:

import matplotlib
matplotlib.pyplot.plot(...)

Or:

import pylab
pylab.plot(...)

Both of these use matplotlib.

Which is recommend as the correct method to plot? Why?

like image 294
Ashwin Nanjappa Avatar asked May 31 '13 03:05

Ashwin Nanjappa


People also ask

Is PyLab and matplotlib same?

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.

Why PyLab is used in Python?

PyLab is a Python package that provides us a namespace in Python programming, which is very similar to MATLAB interface, by importing the functions from Python Numpy and Matplotlib Module.

When should I use matplotlib?

It provides a user to visualize data using a variety of different types of plots to make data understandable. You can use, these different types of plots (scatterplots, histograms, bar charts, errorcharts, boxplots, etc.) by writing few lines of code in python.

What is the difference between matplotlib and matplotlib?

Matplotlib is the whole package; pylab is a module in matplotlib that gets installed alongside matplotlib; and matplotlib. pyplot is a module in matplotlib. Pyplot provides the state-machine interface to the underlying plotting library in matplotlib.


2 Answers

Official docs: Matplotlib, pyplot and pylab: how are they related?

Both of those imports boil down do doing exactly the same thing and will run the exact same code, it is just different ways of importing the modules.

Also note that matplotlib has two interface layers, a state-machine layer managed by pyplot and the OO interface pyplot is built on top of, see How can I attach a pyplot function to a figure instance?

pylab is a clean way to bulk import a whole slew of helpful functions (the pyplot state machine function, most of numpy) into a single name space. The main reason this exists (to my understanding) is to work with ipython to make a very nice interactive shell which more-or-less replicates MATLAB (to make the transition easier and because it is good for playing around). See pylab.py and matplotlib/pylab.py

At some level, this is purely a matter of taste and depends a bit on what you are doing.

If you are not embedding in a gui (either using a non-interactive backend for bulk scripts or using one of the provided interactive backends) the typical thing to do is

import matplotlib.pyplot as plt
import numpy as np

plt.plot(....)

which doesn't pollute the name space. I prefer this so I can keep track of where stuff came from.

If you use

ipython --pylab

this is equivalent to running

from pylab import * 

It is now recommended that for new versions of ipython you use

ipython --matplotlib

which will set up all the proper background details to make the interactive backends to work nicely, but will not bulk import anything. You will need to explicitly import the modules want.

import numpy as np
import matplotlib.pyplot as plt

is a good start.

If you are embedding matplotlib in a gui you don't want to import pyplot as that will start extra gui main loops, and exactly what you should import depends on exactly what you are doing.

like image 128
tacaswell Avatar answered Oct 12 '22 23:10

tacaswell


From the official documentation, as shown below, the recommendation is to use matplotlib.pyplot. This is not an opinion.

The documentation at Matplotlib, pyplot and pylab: how are they related?, which also describes the difference between pyplot and pylab, states: "Although many examples use pylab, it is no longer recommended.".

2021-05-06 Edit:

  • From The pylab API (disapproved)

Since heavily importing into the global namespace may result in unexpected behavior, the use of pylab is strongly discouraged. Use matplotlib.pyplot instead.

like image 41
Apostolos Avatar answered Oct 12 '22 23:10

Apostolos