Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d18baf6

Browse files
committed
Deprecate non-spectral mlab methods
1 parent a456bd1 commit d18baf6

File tree

9 files changed

+251
-106
lines changed

9 files changed

+251
-106
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,7 +4839,7 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
48394839
x, y1, y2 = np.broadcast_arrays(np.atleast_1d(x), y1, y2)
48404840

48414841
polys = []
4842-
for ind0, ind1 in mlab.contiguous_regions(where):
4842+
for ind0, ind1 in cbook.contiguous_regions(where):
48434843
xslice = x[ind0:ind1]
48444844
y1slice = y1[ind0:ind1]
48454845
y2slice = y2[ind0:ind1]
@@ -4998,7 +4998,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49984998
y, x1, x2 = np.broadcast_arrays(np.atleast_1d(y), x1, x2)
49994999

50005000
polys = []
5001-
for ind0, ind1 in mlab.contiguous_regions(where):
5001+
for ind0, ind1 in cbook.contiguous_regions(where):
50025002
yslice = y[ind0:ind1]
50035003
x1slice = x1[ind0:ind1]
50045004
x2slice = x2[ind0:ind1]

lib/matplotlib/cbook/__init__.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,48 @@ def simple_linear_interpolation(a, steps):
15781578
.reshape((len(x),) + a.shape[1:]))
15791579

15801580

1581+
def less_simple_linear_interpolation(x, y, xi, extrap=False):
1582+
"""
1583+
This function provides simple (but somewhat less so than
1584+
:func:`cbook.simple_linear_interpolation`) linear interpolation.
1585+
:func:`simple_linear_interpolation` will give a list of point
1586+
between a start and an end, while this does true linear
1587+
interpolation at an arbitrary set of points.
1588+
1589+
This is very inefficient linear interpolation meant to be used
1590+
only for a small number of points in relatively non-intensive use
1591+
cases. For real linear interpolation, use scipy.
1592+
"""
1593+
x = np.asarray(x)
1594+
y = np.asarray(y)
1595+
xi = np.atleast_1d(xi)
1596+
1597+
s = list(y.shape)
1598+
s[0] = len(xi)
1599+
yi = np.tile(np.nan, s)
1600+
1601+
for ii, xx in enumerate(xi):
1602+
bb = x == xx
1603+
if np.any(bb):
1604+
jj, = np.nonzero(bb)
1605+
yi[ii] = y[jj[0]]
1606+
elif xx < x[0]:
1607+
if extrap:
1608+
yi[ii] = y[0]
1609+
elif xx > x[-1]:
1610+
if extrap:
1611+
yi[ii] = y[-1]
1612+
else:
1613+
jj, = np.nonzero(x < xx)
1614+
jj = max(jj)
1615+
1616+
yi[ii] = (y[jj] +
1617+
(xx - x[jj]) / (x[jj + 1] - x[jj]) *
1618+
(y[jj + 1] - y[jj]))
1619+
1620+
return yi
1621+
1622+
15811623
@deprecated('2.1', alternative='shutil.rmtree')
15821624
def recursive_remove(path):
15831625
if os.path.isdir(path):
@@ -1937,6 +1979,7 @@ def unmasked_index_ranges(mask, compressed=True):
19371979
ls_mapper_r = {v: k for k, v in six.iteritems(ls_mapper)}
19381980

19391981

1982+
@deprecated('2.2')
19401983
def align_iterators(func, *iterables):
19411984
"""
19421985
This generator takes a bunch of iterables that are ordered by func
@@ -1981,6 +2024,32 @@ def __call__(self, key):
19812024
break
19822025

19832026

2027+
def contiguous_regions(mask):
2028+
"""
2029+
Return a list of (ind0, ind1) such that mask[ind0:ind1].all() is
2030+
True and we cover all such regions
2031+
"""
2032+
mask = np.asarray(mask, dtype=bool)
2033+
2034+
if not mask.size:
2035+
return []
2036+
2037+
# Find the indices of region changes, and correct offset
2038+
idx, = np.nonzero(mask[:-1] != mask[1:])
2039+
idx += 1
2040+
2041+
# List operations are faster for moderately sized arrays
2042+
idx = idx.tolist()
2043+
2044+
# Add first and/or last index if needed
2045+
if mask[0]:
2046+
idx = [0] + idx
2047+
if mask[-1]:
2048+
idx.append(len(mask))
2049+
2050+
return list(zip(idx[::2], idx[1::2]))
2051+
2052+
19842053
def is_math_text(s):
19852054
# Did we find an even number of non-escaped dollar signs?
19862055
# If so, treat is as math text.

lib/matplotlib/collections.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from matplotlib.artist import allow_rasterization
3333
import matplotlib.path as mpath
3434
from matplotlib import _path
35-
import matplotlib.mlab as mlab
3635
import matplotlib.lines as mlines
3736

3837
CIRCLE_AREA_FACTOR = 1.0 / np.sqrt(np.pi)
@@ -1052,7 +1051,7 @@ def span_where(x, ymin, ymax, where, **kwargs):
10521051
passed on to the collection.
10531052
"""
10541053
xranges = []
1055-
for ind0, ind1 in mlab.contiguous_regions(where):
1054+
for ind0, ind1 in cbook.contiguous_regions(where):
10561055
xslice = x[ind0:ind1]
10571056
if not len(xslice):
10581057
continue

lib/matplotlib/contour.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import matplotlib.font_manager as font_manager
2222
import matplotlib.text as text
2323
import matplotlib.cbook as cbook
24-
import matplotlib.mlab as mlab
2524
import matplotlib.mathtext as mathtext
2625
import matplotlib.patches as mpatches
2726
import matplotlib.texmanager as texmanager
@@ -381,7 +380,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
381380
not empty (lc defaults to the empty list if None). *spacing*
382381
is the space around the label in pixels to leave empty.
383382
384-
Do both of these tasks at once to avoid calling mlab.path_length
383+
Do both of these tasks at once to avoid calculating path lengths
385384
multiple times, which is relatively costly.
386385
387386
The method used here involves calculating the path length
@@ -396,7 +395,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
396395
hlw = lw / 2.0
397396

398397
# Check if closed and, if so, rotate contour so label is at edge
399-
closed = mlab.is_closed_polygon(slc)
398+
closed = _is_closed_polygon(slc)
400399
if closed:
401400
slc = np.r_[slc[ind:-1], slc[:ind + 1]]
402401

@@ -405,8 +404,10 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
405404

406405
ind = 0
407406

408-
# Path length in pixel space
409-
pl = mlab.path_length(slc)
407+
# Calculate path lengths
408+
pl = np.zeros(slc.shape[0], dtype=float)
409+
dx = np.diff(slc, axis=0)
410+
pl[1:] = np.cumsum(np.hypot(dx[:, 0], dx[:, 1]))
410411
pl = pl - pl[ind]
411412

412413
# Use linear interpolation to get points around label
@@ -631,7 +632,7 @@ def labels(self, inline, inline_spacing):
631632
# zero in print_label and locate_label. Other than these
632633
# functions, this is not necessary and should probably be
633634
# eventually removed.
634-
if mlab.is_closed_polygon(lc):
635+
if _is_closed_polygon(lc):
635636
slc = np.r_[slc0, slc0[1:2, :]]
636637
else:
637638
slc = slc0
@@ -692,6 +693,15 @@ def _find_closest_point_on_leg(p1, p2, p0):
692693
return d, pc
693694

694695

696+
def _is_closed_polygon(X):
697+
"""
698+
Tests whether first and last object in a sequence are the same. These are
699+
presumably coordinates on a polygonal curve, in which case this function
700+
tests if that curve is closed.
701+
"""
702+
return np.all(X[0] == X[-1])
703+
704+
695705
def _find_closest_point_on_path(lc, point):
696706
"""
697707
lc: coordinates of vertices
@@ -706,7 +716,7 @@ def _find_closest_point_on_path(lc, point):
706716
xcmin = None
707717
legmin = (None, None)
708718

709-
closed = mlab.is_closed_polygon(lc)
719+
closed = _is_closed_polygon(lc)
710720

711721
# build list of legs before and after this vertex
712722
legs = []

0 commit comments

Comments
 (0)