Skip to content

Commit e4114e7

Browse files
committed
Deprecate non-spectral mlab methods
1 parent db1cde0 commit e4114e7

File tree

9 files changed

+252
-106
lines changed

9 files changed

+252
-106
lines changed

lib/matplotlib/axes/_axes.py

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

50535053
polys = []
5054-
for ind0, ind1 in mlab.contiguous_regions(where):
5054+
for ind0, ind1 in cbook.contiguous_regions(where):
50555055
xslice = x[ind0:ind1]
50565056
y1slice = y1[ind0:ind1]
50575057
y2slice = y2[ind0:ind1]
@@ -5232,7 +5232,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
52325232
y, x1, x2 = np.broadcast_arrays(np.atleast_1d(y), x1, x2)
52335233

52345234
polys = []
5235-
for ind0, ind1 in mlab.contiguous_regions(where):
5235+
for ind0, ind1 in cbook.contiguous_regions(where):
52365236
yslice = y[ind0:ind1]
52375237
x1slice = x1[ind0:ind1]
52385238
x2slice = x2[ind0:ind1]

lib/matplotlib/cbook/__init__.py

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

15961596

1597+
def less_simple_linear_interpolation(x, y, xi, extrap=False):
1598+
"""
1599+
This function provides simple (but somewhat less so than
1600+
:func:`cbook.simple_linear_interpolation`) linear interpolation.
1601+
:func:`simple_linear_interpolation` will give a list of point
1602+
between a start and an end, while this does true linear
1603+
interpolation at an arbitrary set of points.
1604+
1605+
This is very inefficient linear interpolation meant to be used
1606+
only for a small number of points in relatively non-intensive use
1607+
cases. For real linear interpolation, use scipy.
1608+
"""
1609+
x = np.asarray(x)
1610+
y = np.asarray(y)
1611+
xi = np.atleast_1d(xi)
1612+
1613+
s = list(y.shape)
1614+
s[0] = len(xi)
1615+
yi = np.tile(np.nan, s)
1616+
1617+
for ii, xx in enumerate(xi):
1618+
bb = x == xx
1619+
if np.any(bb):
1620+
jj, = np.nonzero(bb)
1621+
yi[ii] = y[jj[0]]
1622+
elif xx < x[0]:
1623+
if extrap:
1624+
yi[ii] = y[0]
1625+
elif xx > x[-1]:
1626+
if extrap:
1627+
yi[ii] = y[-1]
1628+
else:
1629+
jj, = np.nonzero(x < xx)
1630+
jj = max(jj)
1631+
1632+
yi[ii] = (y[jj] +
1633+
(xx - x[jj]) / (x[jj + 1] - x[jj]) *
1634+
(y[jj + 1] - y[jj]))
1635+
1636+
return yi
1637+
1638+
15971639
@deprecated('2.1', alternative='shutil.rmtree')
15981640
def recursive_remove(path):
15991641
if os.path.isdir(path):
@@ -1952,6 +1994,7 @@ def unmasked_index_ranges(mask, compressed=True):
19521994
ls_mapper_r = {v: k for k, v in six.iteritems(ls_mapper)}
19531995

19541996

1997+
@deprecated('2.2')
19551998
def align_iterators(func, *iterables):
19561999
"""
19572000
This generator takes a bunch of iterables that are ordered by func
@@ -1996,6 +2039,32 @@ def __call__(self, key):
19962039
break
19972040

19982041

2042+
def contiguous_regions(mask):
2043+
"""
2044+
Return a list of (ind0, ind1) such that mask[ind0:ind1].all() is
2045+
True and we cover all such regions
2046+
"""
2047+
mask = np.asarray(mask, dtype=bool)
2048+
2049+
if not mask.size:
2050+
return []
2051+
2052+
# Find the indices of region changes, and correct offset
2053+
idx, = np.nonzero(mask[:-1] != mask[1:])
2054+
idx += 1
2055+
2056+
# List operations are faster for moderately sized arrays
2057+
idx = idx.tolist()
2058+
2059+
# Add first and/or last index if needed
2060+
if mask[0]:
2061+
idx = [0] + idx
2062+
if mask[-1]:
2063+
idx.append(len(mask))
2064+
2065+
return list(zip(idx[::2], idx[1::2]))
2066+
2067+
19992068
def is_math_text(s):
20002069
# Did we find an even number of non-escaped dollar signs?
20012070
# If so, treat is as math text.

lib/matplotlib/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import numpy as np
2525
import matplotlib as mpl
2626
from . import (_path, artist, cbook, cm, colors as mcolors, docstring,
27-
lines as mlines, mlab, path as mpath, transforms)
27+
lines as mlines, path as mpath, transforms)
2828

2929
CIRCLE_AREA_FACTOR = 1.0 / np.sqrt(np.pi)
3030

@@ -1040,7 +1040,7 @@ def span_where(x, ymin, ymax, where, **kwargs):
10401040
passed on to the collection.
10411041
"""
10421042
xranges = []
1043-
for ind0, ind1 in mlab.contiguous_regions(where):
1043+
for ind0, ind1 in cbook.contiguous_regions(where):
10441044
xslice = x[ind0:ind1]
10451045
if not len(xslice):
10461046
continue

lib/matplotlib/contour.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import matplotlib.font_manager as font_manager
2121
import matplotlib.text as text
2222
import matplotlib.cbook as cbook
23-
import matplotlib.mlab as mlab
2423
import matplotlib.mathtext as mathtext
2524
import matplotlib.patches as mpatches
2625
import matplotlib.texmanager as texmanager
@@ -377,7 +376,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
377376
not empty (lc defaults to the empty list if None). *spacing*
378377
is the space around the label in pixels to leave empty.
379378
380-
Do both of these tasks at once to avoid calling mlab.path_length
379+
Do both of these tasks at once to avoid calculating path lengths
381380
multiple times, which is relatively costly.
382381
383382
The method used here involves calculating the path length
@@ -392,7 +391,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
392391
hlw = lw / 2.0
393392

394393
# Check if closed and, if so, rotate contour so label is at edge
395-
closed = mlab.is_closed_polygon(slc)
394+
closed = _is_closed_polygon(slc)
396395
if closed:
397396
slc = np.r_[slc[ind:-1], slc[:ind + 1]]
398397

@@ -401,8 +400,10 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
401400

402401
ind = 0
403402

404-
# Path length in pixel space
405-
pl = mlab.path_length(slc)
403+
# Calculate path lengths
404+
pl = np.zeros(slc.shape[0], dtype=float)
405+
dx = np.diff(slc, axis=0)
406+
pl[1:] = np.cumsum(np.hypot(dx[:, 0], dx[:, 1]))
406407
pl = pl - pl[ind]
407408

408409
# Use linear interpolation to get points around label
@@ -627,7 +628,7 @@ def labels(self, inline, inline_spacing):
627628
# zero in print_label and locate_label. Other than these
628629
# functions, this is not necessary and should probably be
629630
# eventually removed.
630-
if mlab.is_closed_polygon(lc):
631+
if _is_closed_polygon(lc):
631632
slc = np.r_[slc0, slc0[1:2, :]]
632633
else:
633634
slc = slc0
@@ -688,6 +689,15 @@ def _find_closest_point_on_leg(p1, p2, p0):
688689
return d, pc
689690

690691

692+
def _is_closed_polygon(X):
693+
"""
694+
Tests whether first and last object in a sequence are the same. These are
695+
presumably coordinates on a polygonal curve, in which case this function
696+
tests if that curve is closed.
697+
"""
698+
return np.all(X[0] == X[-1])
699+
700+
691701
def _find_closest_point_on_path(lc, point):
692702
"""
693703
lc: coordinates of vertices
@@ -702,7 +712,7 @@ def _find_closest_point_on_path(lc, point):
702712
xcmin = None
703713
legmin = (None, None)
704714

705-
closed = mlab.is_closed_polygon(lc)
715+
closed = _is_closed_polygon(lc)
706716

707717
# build list of legs before and after this vertex
708718
legs = []

0 commit comments

Comments
 (0)