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

Skip to content

Commit 9422409

Browse files
authored
Merge pull request #7488 from anntzer/clip-ptp
Cleanups: np.clip and np.ptp are awesome
2 parents a19ed84 + 6c96216 commit 9422409

File tree

9 files changed

+32
-53
lines changed

9 files changed

+32
-53
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,10 +3401,11 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
34013401
Sets the positions of the boxes. The ticks and limits
34023402
are automatically set to match the positions.
34033403
3404-
widths : array-like, default = 0.5
3404+
widths : array-like, default = None
34053405
Either a scalar or a vector and sets the width of each
3406-
box. The default is 0.5, or ``0.15*(distance between extreme
3407-
positions)`` if that is smaller.
3406+
box. The default is ``0.15*(distance between extreme
3407+
positions)``, clipped to no less than 0.15 and no more than
3408+
0.5.
34083409
34093410
vert : bool, default = False
34103411
If `True` (default), makes the boxes vertical. If `False`,
@@ -3641,8 +3642,7 @@ def dopatch(xs, ys, **kwargs):
36413642

36423643
# width
36433644
if widths is None:
3644-
distance = max(positions) - min(positions)
3645-
widths = [min(0.15 * max(distance, 1.0), 0.5)] * N
3645+
widths = [np.clip(0.15 * np.ptp(positions), 0.15, 0.5)] * N
36463646
elif np.isscalar(widths):
36473647
widths = [widths] * N
36483648
elif len(widths) != N:

lib/matplotlib/backend_bases.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,10 @@ def dynamic_update(self):
27922792
self.canvas.draw_idle()
27932793

27942794
def draw_rubberband(self, event, x0, y0, x1, y1):
2795-
"""Draw a rectangle rubberband to indicate zoom limits."""
2795+
"""Draw a rectangle rubberband to indicate zoom limits.
2796+
2797+
Note that it is not guaranteed that ``x0 <= x1`` and ``y0 <= y1``.
2798+
"""
27962799

27972800
def remove_rubberband(self):
27982801
"""Remove the rubberband."""
@@ -3041,20 +3044,13 @@ def drag_zoom(self, event):
30413044
if self._xypress:
30423045
x, y = event.x, event.y
30433046
lastx, lasty, a, ind, view = self._xypress[0]
3044-
3045-
# adjust x, last, y, last
3046-
x1, y1, x2, y2 = a.bbox.extents
3047-
x, lastx = max(min(x, lastx), x1), min(max(x, lastx), x2)
3048-
y, lasty = max(min(y, lasty), y1), min(max(y, lasty), y2)
3049-
3047+
(x1, y1), (x2, y2) = np.clip(
3048+
[[lastx, lasty], [x, y]], a.bbox.min, a.bbox.max)
30503049
if self._zoom_mode == "x":
3051-
x1, y1, x2, y2 = a.bbox.extents
3052-
y, lasty = y1, y2
3050+
y1, y2 = a.bbox.intervaly
30533051
elif self._zoom_mode == "y":
3054-
x1, y1, x2, y2 = a.bbox.extents
3055-
x, lastx = x1, x2
3056-
3057-
self.draw_rubberband(event, x, y, lastx, lasty)
3052+
x1, x2 = a.bbox.intervalx
3053+
self.draw_rubberband(event, x1, y1, x2, y2)
30583054

30593055
def release_zoom(self, event):
30603056
"""Callback for mouse button release in zoom to rect mode."""

lib/matplotlib/backend_tools.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -895,23 +895,15 @@ def _mouse_move(self, event):
895895

896896
if self._xypress:
897897
x, y = event.x, event.y
898-
lastx, lasty, a, _ind, _view = self._xypress[0]
899-
900-
# adjust x, last, y, last
901-
x1, y1, x2, y2 = a.bbox.extents
902-
x, lastx = max(min(x, lastx), x1), min(max(x, lastx), x2)
903-
y, lasty = max(min(y, lasty), y1), min(max(y, lasty), y2)
904-
898+
lastx, lasty, a, ind, view = self._xypress[0]
899+
(x1, y1), (x2, y2) = np.clip(
900+
[[lastx, lasty], [x, y]], a.bbox.min, a.bbox.max)
905901
if self._zoom_mode == "x":
906-
x1, y1, x2, y2 = a.bbox.extents
907-
y, lasty = y1, y2
902+
y1, y2 = a.bbox.intervaly
908903
elif self._zoom_mode == "y":
909-
x1, y1, x2, y2 = a.bbox.extents
910-
x, lastx = x1, x2
911-
912-
self.toolmanager.trigger_tool('rubberband',
913-
self,
914-
data=(x, y, lastx, lasty))
904+
x1, x2 = a.bbox.intervalx
905+
self.toolmanager.trigger_tool(
906+
'rubberband', self, data=(x1, y1, x2, y2))
915907

916908
def _release(self, event):
917909
"""the release mouse button callback in zoom to rect mode"""

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,7 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
684684
height = self.canvas.figure.bbox.height
685685
y1 = height - y1
686686
y0 = height - y0
687-
688-
w = abs(x1 - x0)
689-
h = abs(y1 - y0)
690-
691-
rect = [int(val) for val in (min(x0, x1), min(y0, y1), w, h)]
687+
rect = [int(val) for val in (x0, y0, x1 - x0, y1 - y0)]
692688
self.canvas.drawRectangle(rect)
693689

694690
def remove_rubberband(self):

lib/matplotlib/patches.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,8 +1271,6 @@ def __init__(self, x, y, dx, dy, width=0.001, length_includes_head=False,
12711271

12721272
docstring.interpd.update({"FancyArrow": FancyArrow.__init__.__doc__})
12731273

1274-
docstring.interpd.update({"FancyArrow": FancyArrow.__init__.__doc__})
1275-
12761274

12771275
class YAArrow(Patch):
12781276
"""

lib/matplotlib/quiver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ def _init(self):
523523
(ax.bbox.width, ax.bbox.height))
524524
self.span = sx
525525
if self.width is None:
526-
sn = max(8, min(25, math.sqrt(self.N)))
526+
sn = np.clip(math.sqrt(self.N), 8, 25)
527527
self.width = 0.06 * self.span / sn
528528

529529
# _make_verts sets self.scale if not already specified

lib/matplotlib/ticker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,8 @@ def set_params(self, **kwargs):
18751875
def _raw_ticks(self, vmin, vmax):
18761876
if self._nbins == 'auto':
18771877
if self.axis is not None:
1878-
nbins = max(min(self.axis.get_tick_space(), 9),
1879-
max(1, self._min_n_ticks - 1))
1878+
nbins = np.clip(self.axis.get_tick_space(),
1879+
max(1, self._min_n_ticks - 1), 9)
18801880
else:
18811881
nbins = 9
18821882
else:
@@ -2074,7 +2074,7 @@ def __call__(self):
20742074
def tick_values(self, vmin, vmax):
20752075
if self.numticks == 'auto':
20762076
if self.axis is not None:
2077-
numticks = max(min(self.axis.get_tick_space(), 9), 2)
2077+
numticks = np.clip(self.axis.get_tick_space(), 2, 9)
20782078
else:
20792079
numticks = 9
20802080
else:

lib/matplotlib/tri/triinterpolate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,10 @@ def __init__(self, triangulation, z, kind='min_E', trifinder=None,
417417
self._z = self._z[~node_mask]
418418

419419
# Computing scale factors
420-
self._unit_x = np.max(compressed_x) - np.min(compressed_x)
421-
self._unit_y = np.max(compressed_y) - np.min(compressed_y)
422-
self._pts = np.vstack((compressed_x/float(self._unit_x),
423-
compressed_y/float(self._unit_y))).T
420+
self._unit_x = np.ptp(compressed_x)
421+
self._unit_y = np.ptp(compressed_y)
422+
self._pts = np.column_stack([compressed_x / self._unit_x,
423+
compressed_y / self._unit_y])
424424
# Computing triangle points
425425
self._tris_pts = self._pts[self._triangles]
426426
# Computing eccentricities

lib/matplotlib/tri/tritools.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ def scale_factors(self):
5050
compressed_triangles = self._triangulation.get_masked_triangles()
5151
node_used = (np.bincount(np.ravel(compressed_triangles),
5252
minlength=self._triangulation.x.size) != 0)
53-
x = self._triangulation.x[node_used]
54-
y = self._triangulation.y[node_used]
55-
ux = np.max(x)-np.min(x)
56-
uy = np.max(y)-np.min(y)
57-
return (1./float(ux), 1./float(uy))
53+
return (1 / np.ptp(self._triangulation.x[node_used]),
54+
1 / np.ptp(self._triangulation.y[node_used]))
5855

5956
def circle_ratios(self, rescale=True):
6057
"""

0 commit comments

Comments
 (0)