Skip to content

less_simple_linear_interpolation can be replaced by np.interp. #9865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/api_changes/2017-11-31-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deprecations
````````````

The now unused ``mlab.less_simple_linear_interpolation`` function is
deprecated.
61 changes: 20 additions & 41 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,63 +417,42 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
else:
dp = np.zeros_like(xi)

ll = mlab.less_simple_linear_interpolation(pl, slc, dp + xi,
extrap=True)

# get vector in pixel space coordinates from one point to other
dd = np.diff(ll, axis=0).ravel()

# Get angle of vector - must be calculated in pixel space for
# text rotation to work correctly
if np.all(dd == 0): # Must deal with case of zero length label
rotation = 0.0
else:
rotation = np.rad2deg(np.arctan2(dd[1], dd[0]))
# Get angle of vector between the two ends of the label - must be
# calculated in pixel space for text rotation to work correctly.
(dx,), (dy,) = (np.diff(np.interp(dp + xi, pl, slc_col))
for slc_col in slc.T)
rotation = np.rad2deg(np.arctan2(dy, dx))

if self.rightside_up:
# Fix angle so text is never upside-down
if rotation > 90:
rotation = rotation - 180.0
if rotation < -90:
rotation = 180.0 + rotation
rotation = (rotation + 90) % 180 - 90

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

# Get indices near points of interest
I = mlab.less_simple_linear_interpolation(
pl, np.arange(len(pl)), xi, extrap=False)

# If those indices aren't beyond contour edge, find x,y
if (not np.isnan(I[0])) and int(I[0]) != I[0]:
xy1 = mlab.less_simple_linear_interpolation(
pl, lc, [xi[0]])

if (not np.isnan(I[1])) and int(I[1]) != I[1]:
xy2 = mlab.less_simple_linear_interpolation(
pl, lc, [xi[1]])

# Round to integer values but keep as float
# To allow check against nan below
# Ignore nans here to avoid throwing an error on Appveyor build
# (can possibly be removed when build uses numpy 1.13)
with np.errstate(invalid='ignore'):
I = [np.floor(I[0]), np.ceil(I[1])]
# Get (integer) indices near points of interest; use -1 as marker
# for out of bounds.
I = np.interp(xi, pl, np.arange(len(pl)), left=-1, right=-1)
I = [np.floor(I[0]).astype(int), np.ceil(I[1]).astype(int)]
if I[0] != -1:
xy1 = [np.interp(xi[0], pl, lc_col) for lc_col in lc.T]
if I[1] != -1:
xy2 = [np.interp(xi[1], pl, lc_col) for lc_col in lc.T]

# Actually break contours
if closed:
# This will remove contour if shorter than label
if np.all(~np.isnan(I)):
nlc.append(np.r_[xy2, lc[int(I[1]):int(I[0]) + 1], xy1])
if np.all(I != -1):
nlc.append(np.row_stack([xy2, lc[I[1]:I[0]+1], xy1]))
else:
# These will remove pieces of contour if they have length zero
if not np.isnan(I[0]):
nlc.append(np.r_[lc[:int(I[0]) + 1], xy1])
if not np.isnan(I[1]):
nlc.append(np.r_[xy2, lc[int(I[1]):]])
if I[0] != -1:
nlc.append(np.row_stack([lc[:I[0]+1], xy1]))
if I[1] != -1:
nlc.append(np.row_stack([xy2, lc[I[1]:]]))

# The current implementation removes contours completely
# covered by labels. Uncomment line below to keep
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,7 @@ def griddata(x, y, z, xi, yi, interp='nn'):
##################################################
# Linear interpolation algorithms
##################################################
@cbook.deprecated("2.2", alternative="np.interp")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs API changes docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

def less_simple_linear_interpolation(x, y, xi, extrap=False):
"""
This function provides simple (but somewhat less so than
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_patheffects.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_SimplePatchShadow_offset():
assert pe._offset == (4, 5)


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