Skip to content

Commit dd1c386

Browse files
committed
Use np.hypot whereever possible.
Except examples, where it may be too obscure? Also added -T to CI sphinx-build invocation, to help troubleshooting e.g. https://circleci.com/gh/anntzer/matplotlib/2505?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link (show traceback on sphinx failure).
1 parent 9869fd7 commit dd1c386

File tree

23 files changed

+66
-80
lines changed

23 files changed

+66
-80
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ mpl-run: &mpl-install
7474

7575
doc-run: &doc-build
7676
name: Build documentation
77-
command: make html
77+
command: make html O=-T
7878
working_directory: doc
7979

8080
doc-bundle-run: &doc-bundle

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ per-file-ignores =
108108
examples/event_handling/poly_editor.py: E501
109109
examples/event_handling/viewlims.py: E501
110110
examples/images_contours_and_fields/affine_image.py: E402
111-
examples/images_contours_and_fields/barb_demo.py: E402, E501
111+
examples/images_contours_and_fields/barb_demo.py: E402
112112
examples/images_contours_and_fields/barcode_demo.py: E402
113113
examples/images_contours_and_fields/contour_corner_mask.py: E402
114114
examples/images_contours_and_fields/contour_demo.py: E402, E501

examples/event_handling/pick_event_demo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ def line_picker(line, mouseevent):
126126
xdata = line.get_xdata()
127127
ydata = line.get_ydata()
128128
maxd = 0.05
129-
d = np.sqrt((xdata - mouseevent.xdata)**2. + (ydata - mouseevent.ydata)**2.)
129+
d = np.sqrt(
130+
(xdata - mouseevent.xdata)**2 + (ydata - mouseevent.ydata)**2)
130131

131132
ind = np.nonzero(np.less_equal(d, maxd))
132133
if len(ind):

examples/images_contours_and_fields/barb_demo.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828

2929
# Arbitrary set of vectors, make them longer and change the pivot point
3030
# (point around which they're rotated) to be the middle
31-
axs1[0, 1].barbs(data['x'], data['y'], data['u'], data['v'], length=8, pivot='middle')
31+
axs1[0, 1].barbs(
32+
data['x'], data['y'], data['u'], data['v'], length=8, pivot='middle')
3233

3334
# Showing colormapping with uniform grid. Fill the circle for an empty barb,
3435
# don't round the values, and change some of the size parameters
35-
axs1[1, 0].barbs(X, Y, U, V, np.sqrt(U * U + V * V), fill_empty=True, rounding=False,
36-
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
36+
axs1[1, 0].barbs(
37+
X, Y, U, V, np.sqrt(U ** 2 + V ** 2), fill_empty=True, rounding=False,
38+
sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
3739

3840
# Change colors as well as the increments for parts of the barbs
3941
axs1[1, 1].barbs(data['x'], data['y'], data['u'], data['v'], flagcolor='r',

examples/images_contours_and_fields/contourf_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Z[:nr // 6, :nc // 6] = np.ma.masked
3131

3232
# mask a circle in the middle:
33-
interior = np.sqrt((X**2) + (Y**2)) < 0.5
33+
interior = np.sqrt(X**2 + Y**2) < 0.5
3434
Z[interior] = np.ma.masked
3535

3636
# We are using automatic selection of contour levels;

examples/images_contours_and_fields/plot_streamplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
2121
U = -1 - X**2 + Y
2222
V = 1 + X - Y**2
23-
speed = np.sqrt(U*U + V*V)
23+
speed = np.sqrt(U**2 + V**2)
2424

2525
fig = plt.figure(figsize=(7, 9))
2626
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2])

examples/lines_bars_and_markers/scatter_masked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
y = 0.9 * np.random.rand(N)
2121
area = (20 * np.random.rand(N))**2 # 0 to 10 point radii
2222
c = np.sqrt(area)
23-
r = np.sqrt(x * x + y * y)
23+
r = np.sqrt(x ** 2 + y ** 2)
2424
area1 = np.ma.masked_where(r < r0, area)
2525
area2 = np.ma.masked_where(r >= r0, area)
2626
plt.scatter(x, y, s=area1, marker='^', c=c)

lib/matplotlib/contour.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,9 @@ def print_label(self, linecontour, labelwidth):
230230

231231
def too_close(self, x, y, lw):
232232
"Return *True* if a label is already near this location."
233-
for loc in self.labelXYs:
234-
d = np.sqrt((x - loc[0]) ** 2 + (y - loc[1]) ** 2)
235-
if d < 1.2 * lw:
236-
return True
237-
return False
233+
thresh = (1.2 * lw) ** 2
234+
return any((x - loc[0]) ** 2 + (y - loc[1]) ** 2 < thresh
235+
for loc in self.labelXYs)
238236

239237
def get_label_coords(self, distances, XX, YY, ysize, lw):
240238
"""

lib/matplotlib/lines.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,16 @@ def contains(self, mouseevent):
477477
else:
478478
pixels = self.figure.dpi / 72. * self.pickradius
479479

480-
# the math involved in checking for containment (here and inside of
481-
# segment_hits) assumes that it is OK to overflow. In case the
482-
# application has set the error flags such that an exception is raised
483-
# on overflow, we temporarily set the appropriate error flags here and
484-
# set them back when we are finished.
480+
# The math involved in checking for containment (here and inside of
481+
# segment_hits) assumes that it is OK to overflow, so temporarily set
482+
# the error flags accordingly.
485483
with np.errstate(all='ignore'):
486484
# Check for collision
487485
if self._linestyle in ['None', None]:
488486
# If no line, return the nearby point(s)
489-
d = (xt - mouseevent.x) ** 2 + (yt - mouseevent.y) ** 2
490-
ind, = np.nonzero(np.less_equal(d, pixels ** 2))
487+
ind, = np.nonzero(
488+
(xt - mouseevent.x) ** 2 + (yt - mouseevent.y) ** 2
489+
<= pixels ** 2)
491490
else:
492491
# If line, return the nearby segment(s)
493492
ind = segment_hits(mouseevent.x, mouseevent.y, xt, yt, pixels)

lib/matplotlib/patches.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,10 +1359,8 @@ def get_path(self):
13591359
xb1, yb1, xb2, yb2 = self.getpoints(x1, y1, x2, y2, k1)
13601360

13611361
# a point on the segment 20% of the distance from the tip to the base
1362-
theta = math.atan2(y2 - y1, x2 - x1)
1363-
r = math.sqrt((y2 - y1) ** 2. + (x2 - x1) ** 2.)
1364-
xm = x1 + self.frac * r * math.cos(theta)
1365-
ym = y1 + self.frac * r * math.sin(theta)
1362+
xm = x1 + self.frac * (x2 - x1)
1363+
ym = y1 + self.frac * (y2 - y1)
13661364
xc1, yc1, xc2, yc2 = self.getpoints(x1, y1, xm, ym, k1)
13671365
xd1, yd1, xd2, yd2 = self.getpoints(x1, y1, xm, ym, k2)
13681366

@@ -2915,10 +2913,10 @@ def connect(self, posA, posB):
29152913
codes.append(Path.LINETO)
29162914
else:
29172915
dx1, dy1 = x1 - cx, y1 - cy
2918-
d1 = (dx1 ** 2 + dy1 ** 2) ** .5
2916+
d1 = np.hypot(dx1, dy1)
29192917
f1 = self.rad / d1
29202918
dx2, dy2 = x2 - cx, y2 - cy
2921-
d2 = (dx2 ** 2 + dy2 ** 2) ** .5
2919+
d2 = np.hypot(dx2, dy2)
29222920
f2 = self.rad / d2
29232921
vertices.extend([(cx + dx1 * f1, cy + dy1 * f1),
29242922
(cx, cy),
@@ -3302,7 +3300,7 @@ def transmute(self, path, mutation_size, linewidth):
33023300

33033301
head_length = self.head_length * mutation_size
33043302
head_width = self.head_width * mutation_size
3305-
head_dist = math.sqrt(head_length ** 2 + head_width ** 2)
3303+
head_dist = np.hypot(head_length, head_width)
33063304
cos_t, sin_t = head_length / head_dist, head_width / head_dist
33073305

33083306
# begin arrow

0 commit comments

Comments
 (0)