Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96304

pylab.X vs pylab.plt.X

I noticed that there some names are duplicated in pylab. Say I import pylab. Here are some examples:

E.g. 1 :

pylab.ion()
pylab.plt.ion()

E.g. 2

pylab.figure(1)
pylab.plot.figure(1)

Is there any difference between them? Why have the two of them?

Upvotes: 1

Views: 166

Answers (2)

tacaswell
tacaswell

Reputation: 87376

If you read the source (the entirety of pylab.py excluding the docstring is below)

from __future__ import print_function
import sys, warnings

from matplotlib.cbook import flatten, is_string_like, exception_to_str, \
     silent_list, iterable, dedent

import matplotlib as mpl
# make mpl.finance module available for backwards compatability, in case folks
# using pylab interface depended on not having to import it
import matplotlib.finance

from matplotlib.dates import date2num, num2date,\
        datestr2num, strpdate2num, drange,\
        epoch2num, num2epoch, mx2num,\
        DateFormatter, IndexDateFormatter, DateLocator,\
        RRuleLocator, YearLocator, MonthLocator, WeekdayLocator,\
        DayLocator, HourLocator, MinuteLocator, SecondLocator,\
        rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY, MONTHLY,\
        WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY, relativedelta

import matplotlib.dates  # Do we need this at all?

# bring all the  symbols in so folks can import them from
# pylab in one fell swoop


## We are still importing too many things from mlab; more cleanup is needed.

from matplotlib.mlab import griddata, stineman_interp, slopes, \
    inside_poly, poly_below, poly_between, \
    is_closed_polygon, path_length, distances_along_curve, vector_lengths

from matplotlib.mlab import window_hanning, window_none,  detrend, demean, \
     detrend_mean, detrend_none, detrend_linear, entropy, normpdf, levypdf, \
     find, longest_contiguous_ones, longest_ones, prepca, \
     prctile, prctile_rank, \
     center_matrix, rk4, bivariate_normal, get_xyz_where, \
     get_sparse_matrix, dist, \
     dist_point_to_segment, segments_intersect, fftsurr, movavg, \
     exp_safe, \
     amap, rms_flat, l1norm, l2norm, norm_flat, frange,  identity, \
     base_repr, binary_repr, log2, ispower2, \
     rec_append_fields, rec_drop_fields, rec_join, csv2rec, rec2csv, isvector

import matplotlib.mlab as mlab
import matplotlib.cbook as cbook

from numpy import *
from numpy.fft import *
from numpy.random import *
from numpy.linalg import *

from matplotlib.pyplot import *

# provide the recommended module abbrevs in the pylab namespace
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma

# don't let numpy's datetime hide stdlib
import datetime

if sys.version_info > (2, 6, 0):
    bytes = __builtins__['bytes']

you see that both every thing from pyplot is imported (from matplotlb.pyplot import * and pyplot) and pyplot is imported (import pyplot as plt). You are not seeing two functions, you are seeing the same function/module imported multiple times.

As to why, why not? pylab is designed for interactive use. It is convenient to have all the function directly in your name space and it is quite handy to have plt in the name space as well for prototyping code.

Upvotes: 1

Viktor Kerkez
Viktor Kerkez

Reputation: 46596

You can always check:

>>> pylab.ion is pylab.plt.ion
True

So, they are the same function.

Some names are duplicated, probably historical reasons and to enable backward compatibility...

There is an unwritten convention to import matplotlib as:

import matplotlib.pyplot as plt

if you want just the plotting functionality.

Importing pylab creates a Matlab like environment with a lot of functionalities from NumPy. (so this is also a reason for name duplication)

Upvotes: 2

Related Questions