8000 Merge pull request #14729 from anntzer/moresimple · matplotlib/matplotlib@95f7818 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95f7818

Browse files
authored
Merge pull request #14729 from anntzer/moresimple
Small simplifications.
2 parents ab9fe2f + 7b2cb35 commit 95f7818

File tree

4 files changed

+11
-18
lines changed

4 files changed

+11
-18
lines changed

lib/matplotlib/lines.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,11 @@ def _slice_or_none(in_v, slc):
163163
delta = np.empty((len(disp_coords), 2))
164164
delta[0, :] = 0
165165
delta[1:, :] = disp_coords[1:, :] - disp_coords[:-1, :]
166-
delta = np.sum(delta**2, axis=1)
167-
delta = np.sqrt(delta)
168-
delta = np.cumsum(delta)
166+
delta = np.hypot(*delta.T).cumsum()
169167
# calc distance between markers along path based on the axes
170168
# bounding box diagonal being a distance of unity:
171-
scale = ax_transform.transform(np.array([[0, 0], [1, 1]]))
172-
scale = np.diff(scale, axis=0)
173-
scale = np.sum(scale**2)
174-
scale = np.sqrt(scale)
169+
(x0, y0), (x1, y1) = ax_transform.transform([[0, 0], [1, 1]])
170+
scale = np.hypot(x1 - x0, y1 - y0)
175171
marker_delta = np.arange(start * scale, delta[-1], step * scale)
176172
# find closest actual data point that is closest to
177173
# the theoretical distance along the path:

lib/matplotlib/mlab.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,9 +1484,8 @@ def __init__(self, dataset, bw_method=None):
14841484

14851485
self.covariance = self.data_covariance * self.factor ** 2
14861486
self.inv_cov = self.data_inv_cov / self.factor ** 2
1487-
self.norm_factor = np.sqrt(
1488-
np.linalg.det(
1489-
2 * np.pi * self.covariance)) * self.num_dp
1487+
self.norm_factor = (np.sqrt(np.linalg.det(2 * np.pi * self.covariance))
1488+
* self.num_dp)
14901489

14911490
def scotts_factor(self):
14921491
return np.power(self.num_dp, -1. / (self.dim + 4))

lib/matplotlib/tests/test_collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def test_pathcollection_legend_elements():
691691
l3 = ax.legend(h, l, loc=4)
692692

693693
h, l = sc.legend_elements(prop="sizes", num=4, fmt="{x:.2f}",
694-
func=lambda x: 2*x)
694+
func=lambda x: 2*x)
695695
actsizes = [line.get_markersize() for line in h]
696696
labeledsizes = np.sqrt(np.array(l).astype(float)/2)
697697
assert_array_almost_equal(actsizes, labeledsizes)

lib/matplotlib/widgets.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,15 +1967,13 @@ def set_animated(self, val):
19671967

19681968
def closest(self, x, y):
19691969
"""Return index and pixel distance to closest index."""
1970-
pts = np.transpose((self.x, self.y))
1970+
pts = np.column_stack([self.x, self.y])
19711971
# Transform data coordinates to pixel coordinates.
19721972
pts = self.ax.transData.transform(pts)
1973-
diff = pts - ((x, y))
1974-
if diff.ndim == 2:
1975-
dist = np.sqrt(np.sum(diff ** 2, axis=1))
1976-
return np.argmin(dist), np.min(dist)
1977-
else:
1978-
return 0, np.sqrt(np.sum(diff ** 2))
1973+
diff = pts - [x, y]
1974+
dist = np.hypot(*diff.T)
1975+
min_index = np.argmin(dist)
1976+
return min_index, dist[min_index]
19791977

19801978

19811979
class RectangleSelector(_SelectorWidget):

0 commit comments

Comments
 (0)
0