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

Skip to content

Replace mlab functions #9177

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

Closed
wants to merge 3 commits into from
Closed
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
23 changes: 17 additions & 6 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
not empty (lc defaults to the empty list if None). *spacing*
is the space around the label in pixels to leave empty.

Do both of these tasks at once to avoid calling mlab.path_length
Do both of these tasks at once to avoid calculating path lengths
multiple times, which is relatively costly.

The method used here involves calculating the path length
Expand All @@ -391,7 +391,7 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
hlw = lw / 2.0

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

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

ind = 0

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

# Use linear interpolation to get points around label
Expand Down Expand Up @@ -644,7 +646,7 @@ def labels(self, inline, inline_spacing):
# zero in print_label and locate_label. Other than these
# functions, this is not necessary and should probably be
# eventually removed.
if mlab.is_closed_polygon(lc):
if _is_closed_polygon(lc):
slc = np.r_[slc0, slc0[1:2, :]]
else:
slc = slc0
Expand Down Expand Up @@ -705,6 +707,15 @@ def _find_closest_point_on_leg(p1, p2, p0):
return d, pc


def _is_closed_polygon(X):
"""
Tests whether first and last object in a sequence are the same. These are
presumably coordinates on a polygonal curve, in which case this function
tests if that curve is closed.
"""
return np.all(X[0] == X[-1])


def _find_closest_point_on_path(lc, point):
"""
lc: coordinates of vertices
Expand All @@ -719,7 +730,7 @@ def _find_closest_point_on_path(lc, point):
xcmin = None
legmin = (None, None)

closed = mlab.is_closed_polygon(lc)
closed = _is_closed_polygon(lc)

# build list of legs before and after this vertex
legs = []
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,7 @@ def derivs(x,t):
return yout


@cbook.deprecated('2.2')
def bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0,
mux=0.0, muy=0.0, sigmaxy=0.0):
"""
Expand Down Expand Up @@ -3812,6 +3813,7 @@ def poly_between(x, ylower, yupper):
return x, y


@cbook.deprecated('2.2')
def is_closed_polygon(X):
"""
Tests whether first and last object in a sequence are the same. These are
Expand Down Expand Up @@ -3911,6 +3913,7 @@ def cross_from_above(x, threshold):
##################################################
# Vector and path length geometry calculations
##################################################
@cbook.deprecated('2.2')
def vector_lengths(X, P=2., axis=None):
"""
Finds the length of a set of vectors in *n* dimensions. This is
Expand All @@ -3925,6 +3928,7 @@ def vector_lengths(X, P=2., axis=None):
return (np.sum(X**(P), axis=axis))**(1./P)


@cbook.deprecated('2.2')
def distances_along_curve(X):
"""
Computes the distance between a set of successive points in *N* dimensions.
Expand All @@ -3937,6 +3941,7 @@ def distances_along_curve(X):
return vector_lengths(X, axis=1)


@cbook.deprecated('2.2')
def path_length(X):
"""
Computes the distance travelled along a polygonal curve in *N* dimensions.
Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import datetime

import numpy as np
from matplotlib import mlab
from matplotlib.testing.decorators import image_comparison
from matplotlib import pyplot as plt
from numpy.testing import assert_array_almost_equal
Expand Down Expand Up @@ -243,8 +242,10 @@ def test_labels():
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z1 = np.exp(-(X**2 + Y**2) / 2) / (2 * np.pi)
Z2 = (np.exp(-(((X - 1) / 1.5)**2 + ((Y - 1) / 0.5)**2) / 2) /
(2 * np.pi * 0.5 * 1.5))

# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

Expand Down
11 changes: 6 additions & 5 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from matplotlib import patches
import matplotlib.pyplot as plt

from matplotlib import mlab
import pytest

from copy import copy
Expand Down Expand Up @@ -611,8 +610,9 @@ def test_rotate_image():
delta = 0.25
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z1 = np.exp(-(X**2 + Y**2) / 2) / (2 * np.pi)
Z2 = (np.exp(-(((X - 1) / 1.5)**2 + ((Y - 1) / 0.5)**2) / 2) /
(2 * np.pi * 0.5 * 1.5))
Z = Z2 - Z1 # difference of Gaussians

fig, ax1 = plt.subplots(1, 1)
Expand Down Expand Up @@ -673,8 +673,9 @@ def test_mask_image_over_under():
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z1 = np.exp(-(X**2 + Y**2) / 2) / (2 * np.pi)
Z2 = (np.exp(-(((X - 1) / 1.5)**2 + ((Y - 1) / 0.5)**2) / 2) /
(2 * np.pi * 0.5 * 1.5))
Z = 10*(Z2 - Z1) # difference of Gaussians

palette = copy(plt.cm.gray)
Expand Down