我注意到有些名字在pylab
. 说我import pylab
。这里有些例子:
例如 1:
pylab.ion()
pylab.plt.ion()
例如 2
pylab.figure(1)
pylab.plot.figure(1)
它们之间有什么区别吗?为什么会有他们两个?
我注意到有些名字在pylab
. 说我import pylab
。这里有些例子:
例如 1:
pylab.ion()
pylab.plt.ion()
例如 2
pylab.figure(1)
pylab.plot.figure(1)
它们之间有什么区别吗?为什么会有他们两个?
您可以随时检查:
>>> pylab.ion is pylab.plt.ion
True
因此,它们是相同的功能。
有些名字是重复的,可能是历史原因,为了向后兼容......
有一个不成文的约定导入matplotlib
为:
import matplotlib.pyplot as plt
如果您只想要绘图功能。
导入pylab
会创建一个类似 Matlab 的环境,其中包含 NumPy 的许多功能。(所以这也是名字重复的一个原因)
如果您阅读了源代码(pylab.py
不包括文档字符串的全部内容如下)
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']
您会看到,其中的所有东西pyplot
都是进口的(from matplotlb.pyplot import *
和pyplot
)和pyplot
进口的(import pyplot as plt
)。您没有看到两个函数,而是多次导入相同的函数/模块。
至于为什么,为什么不呢? pylab
专为交互式使用而设计。将所有功能直接放在名称空间中很方便,并且plt
在名称空间中以及原型代码中也非常方便。