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

Skip to content

Commit adb2f8a

Browse files
committed
less_simple_linear_interpolation can be replaced by np.interp.
Also, in contour (the only place that used it), -1 (instead of nan) is a perfectly reasonable marker for out of bounds which avoids having to dance with the errstate.
1 parent c608ae1 commit adb2f8a

File tree

4 files changed

+27
-42
lines changed

4 files changed

+27
-42
lines changed

doc/api/api_changes/2017-11-31-AL.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Deprecations
2+
````````````
3+
4+
The now unused ``mlab.less_simple_linear_interpolation`` function is
5+
deprecated.

lib/matplotlib/contour.py

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -417,63 +417,42 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
417417
else:
418418
dp = np.zeros_like(xi)
419419

420-
ll = mlab.less_simple_linear_interpolation(pl, slc, dp + xi,
421-
extrap=True)
422-
423-
# get vector in pixel space coordinates from one point to other
424-
dd = np.diff(ll, axis=0).ravel()
425-
426-
# Get angle of vector - must be calculated in pixel space for
427-
# text rotation to work correctly
428-
if np.all(dd == 0): # Must deal with case of zero length label
429-
rotation = 0.0
430-
else:
431-
rotation = np.rad2deg(np.arctan2(dd[1], dd[0]))
420+
# Get angle of vector between the two ends of the label - must be
421+
# calculated in pixel space for text rotation to work correctly.
422+
(dx,), (dy,) = (np.diff(np.interp(dp + xi, pl, slc_col))
423+
for slc_col in slc.T)
424+
rotation = np.rad2deg(np.arctan2(dy, dx))
432425

433426
if self.rightside_up:
434427
# Fix angle so text is never upside-down
435-
if rotation > 90:
436-
rotation = rotation - 180.0
437-
if rotation < -90:
438-
rotation = 180.0 + rotation
428+
rotation = (rotation + 90) % 180 - 90
439429

440430
# Break contour if desired
441431
nlc = []
442432
if len(lc):
443433
# Expand range by spacing
444434
xi = dp + xi + np.array([-spacing, spacing])
445435

446-
# Get indices near points of interest
447-
I = mlab.less_simple_linear_interpolation(
448-
pl, np.arange(len(pl)), xi, extrap=False)
449-
450-
# If those indices aren't beyond contour edge, find x,y
451-
if (not np.isnan(I[0])) and int(I[0]) != I[0]:
452-
xy1 = mlab.less_simple_linear_interpolation(
453-
pl, lc, [xi[0]])
454-
455-
if (not np.isnan(I[1])) and int(I[1]) != I[1]:
456-
xy2 = mlab.less_simple_linear_interpolation(
457-
pl, lc, [xi[1]])
458-
459-
# Round to integer values but keep as float
460-
# To allow check against nan below
461-
# Ignore nans here to avoid throwing an error on Appveyor build
462-
# (can possibly be removed when build uses numpy 1.13)
463-
with np.errstate(invalid='ignore'):
464-
I = [np.floor(I[0]), np.ceil(I[1])]
436+
# Get (integer) indices near points of interest; use -1 as marker
437+
# for out of bounds.
438+
I = np.interp(xi, pl, np.arange(len(pl)), left=-1, right=-1)
439+
I = [np.floor(I[0]).astype(int), np.ceil(I[1]).astype(int)]
440+
if I[0] != -1:
441+
xy1 = [np.interp(xi[0], pl, lc_col) for lc_col in lc.T]
442+
if I[1] != -1:
443+
xy2 = [np.interp(xi[1], pl, lc_col) for lc_col in lc.T]
465444

466445
# Actually break contours
467446
if closed:
468447
# This will remove contour if shorter than label
469-
if np.all(~np.isnan(I)):
470-
nlc.append(np.r_[xy2, lc[int(I[1]):int(I[0]) + 1], xy1])
448+
if np.all(I != -1):
449+
nlc.append(np.row_stack([xy2, lc[I[1]:I[0]+1], xy1]))
471450
else:
472451
# These will remove pieces of contour if they have length zero
473-
if not np.isnan(I[0]):
474-
nlc.append(np.r_[lc[:int(I[0]) + 1], xy1])
475-
if not np.isnan(I[1]):
476-
nlc.append(np.r_[xy2, lc[int(I[1]):]])
452+
if I[0] != -1:
453+
nlc.append(np.row_stack([lc[:I[0]+1], xy1]))
454+
if I[1] != -1:
455+
nlc.append(np.row_stack([xy2, lc[I[1]:]]))
477456

478457
# The current implementation removes contours completely
479458
# covered by labels. Uncomment line below to keep

lib/matplotlib/mlab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,7 @@ def griddata(x, y, z, xi, yi, interp='nn'):
34043404
##################################################
34053405
# Linear interpolation algorithms
34063406
##################################################
3407+
@cbook.deprecated("2.2", alternative="np.interp")
34073408
def less_simple_linear_interpolation(x, y, xi, extrap=False):
34083409
"""
34093410
This function provides simple (but somewhat less so than

lib/matplotlib/tests/test_patheffects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_SimplePatchShadow_offset():
122122
assert pe._offset == (4, 5)
123123

124124

125-
@image_comparison(baseline_images=['collection'], tol=0.015)
125+
@image_comparison(baseline_images=['collection'], tol=0.02)
126126
def test_collection():
127127
x, y = np.meshgrid(np.linspace(0, 10, 150), np.linspace(-5, 5, 100))
128128
data = np.sin(x) + np.cos(y)

0 commit comments

Comments
 (0)