Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is %pylab?

I keep seeing people use %pylabin various code snippits, particularly with iPython. However, I cannot see where %pylabis mentioned anywhere in Learning Python (and the few other Python books I have) and am not really sure what it means.

I'm sure the answer is simple, but can anyone enlighten me?

like image 586
Anton Avatar asked Jan 06 '14 23:01

Anton


People also ask

What is PyLab used for?

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.

Why is PyLab discouraged?

pylab is deprecated and its use is strongly discouraged because of namespace pollution. Use pyplot instead. For non-interactive plotting it is suggested to use pyplot to create the figures and then the OO interface for plotting.

Is NumPy in PyLab?

PyLab is actually embedded inside Matplotlib and provides a Matlab®-like experience for the user. It imports portions of Matplotlib and NumPy.

What is %%pylab?

%pylab is a "magic function" that you can call within IPython, or Interactive Python. By invoking it, the IPython interpreter will import matplotlib and NumPy modules such that you'll have convenient access to their functions. As an example,

What is the difference between pylab and Matplotlib?

And, as we’ll learn soon, it does a lot of work for us behind the scenes. pylab, on the other hand, is a module within matplotlib that exists only to bring a number of functions and classes from both numpy and pyplot into the global namespace, making it easy for MATLAB users who were not used to imports to use Matplotlib.

Can I use pylab in Jupyter?

Using PyLab is discouraged now. It's a Python plotting library, inspired by MATLAB, meaning that the terms used (Axis, Figure, Plots) will be similar to those used in MATLAB. It can be used both within a Python system (via the object-oriented API) but also in more convenient form in Jupyter and IPython notebooks via the Pyplot interface.

What is the history of Pyplot and pylab?

The understand Pyplot and PyLab, you need to look at the history of matplotlib, which was originally developed by a neurobiologist named John D. Hunter in the early 2000s. It was inspired by MATLAB and is now a community effort. 00:00 Before we can start coding our graphics, we need to talk about some of the theory behind Matplotlib.


2 Answers

%pylab is a magic function in ipython.

Magic functions in ipython always begin with the percent sign (%) followed without any spaces by a small text string; in essence, ipython magic functions define shortcuts particularly useful for interactive work, e.g., to give you an idea of how magic functions work in python, a few of my favorites:

  • to view cwd directory contents:

    %ls    
  • to run a script in ipython using an empty namespace, type space then a script name:

    %run      
  • to execute a code snippet (particularly for multi-line snippets which would usually cause an _IndentationError_ to be thrown):

    %paste 

When the %pylab magic function is entered at the IPython prompt, it triggers the import of various modules within Matplotlib.

Which modules? well, the ones subsumed under the pylab interface.

The awesome Matplotlib plotting library has two distinct interfaces: a pythonic one, and the original MATLAB-like one intended for plotting at the interactive prompt.

The former is usually imported like so:

from matplotlib import pyplot as PLT 

Indeed, pyplot has its own magic python magic function

%pyplot 

Why two different interfaces? Matplotlib's original interface was pylab; only later was the pythonic interface added. Scripting and app development were not the primary uses cases for Matplotlib when the project began, plotting in the python shell was.

Apparently John Hunter (Matplotlib's creator) wanted to include interactive plotting in python so he submitted a patch to Fernando Perez's (FP) IPython project. FP was a Ph.D student at the time and informed JH that he would not able to review the path for some time. As a result, JH created Matplotlib. The significance is that Matplotlib began as a shell-based plotting scheme.

the pylab interface is indeed more suitable for interactive work:

from pylab import *  x, y = arange(10), cos(x/2) plot(x, y) show() 

and using the pyplot interface:

from matplotlib import pyplot as PLT import numpy as NP  x, y = NP.arange(10), NP.cos(x/2) fig = PLT.figure() ax1 = fig.add_subplot(111) ax1.plot(x, y) PLT.show() 
like image 150
doug Avatar answered Sep 22 '22 22:09

doug


%pylab is a shortcut for typing all of the below commands - in essence adding numpy and matplotlib into your session. This was incorporated in iPython as a transition tool and the current recommendation is that you should not use it. The core reason is that the below sets of commands import too much into the global namespace and also they don't allow you to change the mode for matplotlib from UI to QT or something else. You can see tje history and reasoning behind this at http://nbviewer.ipython.org/github/Carreau/posts/blob/master/10-No-PyLab-Thanks.ipynb?create=1.

This is what %pylab does:

import numpy import matplotlib from matplotlib import pylab, mlab, pyplot np = numpy plt = pyplot  from IPython.core.pylabtools import figsize, getfigs  from pylab import * from numpy import * 

This is what I use instead at the start of my notebook:

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline 
like image 30
Shital Shah Avatar answered Sep 25 '22 22:09

Shital Shah