Skip to content

Commit

Permalink
Backport PR matplotlib#26762: MNT: Numpy 2.0 removals from ndarray class
Browse files Browse the repository at this point in the history
  • Loading branch information
timhoffm authored and meeseeksmachine committed Sep 14, 2023
1 parent 4f15bdf commit 661c871
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,8 @@ def safe_masked_invalid(x, copy=False):
if not x.dtype.isnative:
# If we have already made a copy, do the byteswap in place, else make a
# copy with the byte order swapped.
x = x.byteswap(inplace=copy).newbyteorder('N') # Swap to native order.
# Swap to native order.
x = x.byteswap(inplace=copy).view(x.dtype.newbyteorder('N'))
try:
xm = np.ma.masked_where(~(np.isfinite(x)), x, copy=False)
except TypeError:
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ def __call__(self, X, alpha=None, bytes=False):

xa = np.array(X, copy=True)
if not xa.dtype.isnative:
xa = xa.byteswap().newbyteorder() # Native byteorder is faster.
# Native byteorder is faster.
xa = xa.byteswap().view(xa.dtype.newbyteorder())
if xa.dtype.kind == "f":
xa *= self.N
# xa == 1 (== N after multiplication) is not out of range.
Expand Down Expand Up @@ -2161,7 +2162,7 @@ def rgb_to_hsv(arr):
out = np.zeros_like(arr)
arr_max = arr.max(-1)
ipos = arr_max > 0
delta = arr.ptp(-1)
delta = np.ptp(arr, -1)
s = np.zeros_like(delta)
s[ipos] = delta[ipos] / arr_max[ipos]
ipos = delta > 0
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ def test_pcolormesh():
Qz = np.sin(Y) + np.sin(X)
Qx = (Qx + 1.1)
Z = np.hypot(X, Y) / 5
Z = (Z - Z.min()) / Z.ptp()
Z = (Z - Z.min()) / np.ptp(Z)

# The color array can include masked values:
Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)
Expand All @@ -1348,7 +1348,7 @@ def test_pcolormesh_small():
Qz = np.sin(Y) + np.sin(X)
Qx = (Qx + 1.1)
Z = np.hypot(X, Y) / 5
Z = (Z - Z.min()) / Z.ptp()
Z = (Z - Z.min()) / np.ptp(Z)
Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)
Zm2 = ma.masked_where(Qz < -0.5 * np.max(Qz), Z)

Expand Down Expand Up @@ -1378,7 +1378,7 @@ def test_pcolormesh_alpha():
Qx = X
Qy = Y + np.sin(X)
Z = np.hypot(X, Y) / 5
Z = (Z - Z.min()) / Z.ptp()
Z = (Z - Z.min()) / np.ptp(Z)
vir = mpl.colormaps["viridis"].resampled(16)
# make another colormap with varying alpha
colors = vir(np.arange(16))
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_colormap_endian():
a = [-0.5, 0, 0.5, 1, 1.5, np.nan]
for dt in ["f2", "f4", "f8"]:
anative = np.ma.masked_invalid(np.array(a, dtype=dt))
aforeign = anative.byteswap().newbyteorder()
aforeign = anative.byteswap().view(anative.dtype.newbyteorder())
assert_array_equal(cmap(anative), cmap(aforeign))


Expand Down Expand Up @@ -1126,7 +1126,7 @@ def alternative_hillshade(azimuth, elev, z):

intensity = np.tensordot(normals, illum, axes=(2, 0))
intensity -= intensity.min()
intensity /= intensity.ptp()
intensity /= np.ptp(intensity)
return intensity

y, x = np.mgrid[5:0:-1, :5]
Expand Down

0 comments on commit 661c871

Please sign in to comment.