Skip to content

Commit c8d1656

Browse files
authored
Merge pull request matplotlib#24295 from anntzer/ua
Remove unnecessary np.{,as}array / astype calls.
2 parents c8b9eb4 + 1068a6f commit c8d1656

File tree

20 files changed

+52
-67
lines changed

20 files changed

+52
-67
lines changed

examples/event_handling/path_editor.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,12 @@ def get_ind_under_point(self, event):
8282
Return the index of the point closest to the event position or *None*
8383
if no point is within ``self.epsilon`` to the event position.
8484
"""
85-
# display coords
86-
xy = np.asarray(self.pathpatch.get_path().vertices)
87-
xyt = self.pathpatch.get_transform().transform(xy)
85+
xy = self.pathpatch.get_path().vertices
86+
xyt = self.pathpatch.get_transform().transform(xy) # to display coords
8887
xt, yt = xyt[:, 0], xyt[:, 1]
8988
d = np.sqrt((xt - event.x)**2 + (yt - event.y)**2)
9089
ind = d.argmin()
91-
92-
if d[ind] >= self.epsilon:
93-
ind = None
94-
95-
return ind
90+
return ind if d[ind] < self.epsilon else None
9691

9792
def on_draw(self, event):
9893
"""Callback for draws."""

examples/subplots_axes_and_figures/secondary_axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def rad2deg(x):
5959

6060
def one_over(x):
6161
"""Vectorized 1/x, treating x==0 manually"""
62-
x = np.array(x).astype(float)
62+
x = np.array(x, float)
6363
near_zero = np.isclose(x, 0)
6464
x[near_zero] = np.inf
6565
x[~near_zero] = 1 / x[~near_zero]

examples/units/basic_units.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def __getattribute__(self, name):
144144
return object.__getattribute__(self, name)
145145

146146
def __array__(self, dtype=object):
147-
return np.asarray(self.value).astype(dtype)
147+
return np.asarray(self.value, dtype)
148148

149149
def __array_wrap__(self, array, context):
150150
return TaggedValue(array, self.unit)

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ def on_pick(event):
14641464
line = event.artist
14651465
xdata, ydata = line.get_data()
14661466
ind = event.ind
1467-
print('on pick line:', np.array([xdata[ind], ydata[ind]]).T)
1467+
print(f'on pick line: {xdata[ind]:.3f}, {ydata[ind]:.3f}')
14681468
14691469
cid = fig.canvas.mpl_connect('pick_event', on_pick)
14701470
"""

lib/matplotlib/collections.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ def set_offsets(self, offsets):
547547
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
548548
offsets = offsets[None, :]
549549
self._offsets = np.column_stack(
550-
(np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
551-
np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))
550+
(np.asarray(self.convert_xunits(offsets[:, 0]), float),
551+
np.asarray(self.convert_yunits(offsets[:, 1]), float)))
552552
self.stale = True
553553

554554
def get_offsets(self):
@@ -573,7 +573,7 @@ def set_linewidth(self, lw):
573573
if lw is None:
574574
lw = self._get_default_linewidth()
575575
# get the un-scaled/broadcast lw
576-
self._us_lw = np.atleast_1d(np.asarray(lw))
576+
self._us_lw = np.atleast_1d(lw)
577577

578578
# scale all of the dash patterns.
579579
self._linewidths, self._linestyles = self._bcast_lwls(

lib/matplotlib/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ def _process_contour_level_args(self, args):
11441144
if isinstance(levels_arg, Integral):
11451145
self.levels = self._autolev(levels_arg)
11461146
else:
1147-
self.levels = np.asarray(levels_arg).astype(np.float64)
1147+
self.levels = np.asarray(levels_arg, np.float64)
11481148

11491149
if not self.filled:
11501150
inside = (self.levels > self.zmin) & (self.levels < self.zmax)

lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ def _pil_png_to_float_array(pil_png):
17011701
mode = pil_png.mode
17021702
rawmode = pil_png.png.im_rawmode
17031703
if rawmode == "1": # Grayscale.
1704-
return np.asarray(pil_png).astype(np.float32)
1704+
return np.asarray(pil_png, np.float32)
17051705
if rawmode == "L;2": # Grayscale.
17061706
return np.divide(pil_png, 2**2 - 1, dtype=np.float32)
17071707
if rawmode == "L;4": # Grayscale.

lib/matplotlib/patches.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,7 @@ def _recompute_path(self):
12211221
arc.codes, [connector, connector, Path.CLOSEPOLY]])
12221222

12231223
# Shift and scale the wedge to the final location.
1224-
v *= self.r
1225-
v += np.asarray(self.center)
1226-
self._path = Path(v, c)
1224+
self._path = Path(v * self.r + self.center, c)
12271225

12281226
def set_center(self, center):
12291227
self._path = None

lib/matplotlib/sankey.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
434434
Sankey.finish
435435
"""
436436
# Check and preprocess the arguments.
437-
if flows is None:
438-
flows = np.array([1.0, -1.0])
439-
else:
440-
flows = np.array(flows)
437+
flows = np.array([1.0, -1.0]) if flows is None else np.array(flows)
441438
n = flows.shape[0] # Number of flows
442439
if rotation is None:
443440
rotation = 0

lib/matplotlib/testing/compare.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ def save_diff_image(expected, actual, output):
500500
actual_image = _load_image(actual)
501501
actual_image, expected_image = crop_to_same(
502502
actual, actual_image, expected, expected_image)
503-
expected_image = np.array(expected_image).astype(float)
504-
actual_image = np.array(actual_image).astype(float)
503+
expected_image = np.array(expected_image, float)
504+
actual_image = np.array(actual_image, float)
505505
if expected_image.shape != actual_image.shape:
506506
raise ImageComparisonFailure(
507507
"Image sizes do not match expected size: {} "

0 commit comments

Comments
 (0)