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

Skip to content

Commit 8799411

Browse files
anntzertimhoffm
authored andcommitted
Replace assignments to array.shape by calls to reshape(). (#12798)
1 parent 3e90025 commit 8799411

3 files changed

Lines changed: 17 additions & 29 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,21 +1317,19 @@ def rgb_to_hsv(arr):
13171317
hsv : (..., 3) ndarray
13181318
Colors converted to hsv values in range [0, 1]
13191319
"""
1320-
# make sure it is an ndarray
13211320
arr = np.asarray(arr)
13221321

13231322
# check length of the last dimension, should be _some_ sort of rgb
13241323
if arr.shape[-1] != 3:
13251324
raise ValueError("Last dimension of input array must be 3; "
13261325
"shape {} was found.".format(arr.shape))
13271326

1328-
in_ndim = arr.ndim
1329-
if arr.ndim == 1:
1330-
arr = np.array(arr, ndmin=2)
1331-
1332-
# make sure we don't have an int image
1333-
arr = arr.astype(np.promote_types(arr.dtype, np.float32))
1334-
1327+
in_shape = arr.shape
1328+
arr = np.array(
1329+
arr, copy=False,
1330+
dtype=np.promote_types(arr.dtype, np.float32), # Don't work on ints.
1331+
ndmin=2, # In case input was 1D.
1332+
)
13351333
out = np.zeros_like(arr)
13361334
arr_max = arr.max(-1)
13371335
ipos = arr_max > 0
@@ -1353,10 +1351,7 @@ def rgb_to_hsv(arr):
13531351
out[..., 1] = s
13541352
out[..., 2] = arr_max
13551353

1356-
if in_ndim == 1:
1357-
out.shape = (3,)
1358-
1359-
return out
1354+
return out.reshape(in_shape)
13601355

13611356

13621357
def hsv_to_rgb(hsv):
@@ -1381,14 +1376,12 @@ def hsv_to_rgb(hsv):
13811376
raise ValueError("Last dimension of input array must be 3; "
13821377
"shape {shp} was found.".format(shp=hsv.shape))
13831378

1384-
# if we got passed a 1D array, try to treat as
1385-
# a single color and reshape as needed
1386-
in_ndim = hsv.ndim
1387-
if in_ndim == 1:
1388-
hsv = np.array(hsv, ndmin=2)
1389-
1390-
# make sure we don't have an int image
1391-
hsv = hsv.astype(np.promote_types(hsv.dtype, np.float32))
1379+
in_shape = hsv.shape
1380+
hsv = np.array(
1381+
hsv, copy=False,
1382+
dtype=np.promote_types(hsv.dtype, np.float32), # Don't work on ints.
1383+
ndmin=2, # In case input was 1D.
1384+
)
13921385

13931386
h = hsv[..., 0]
13941387
s = hsv[..., 1]
@@ -1441,10 +1434,7 @@ def hsv_to_rgb(hsv):
14411434

14421435
rgb = np.stack([r, g, b], axis=-1)
14431436

1444-
if in_ndim == 1:
1445-
rgb.shape = (3,)
1446-
1447-
return rgb
1437+
return rgb.reshape(in_shape)
14481438

14491439

14501440
def _vector_magnitude(arr):

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ def __isub__(self, other):
695695
def __add__(self, other):
696696
raise RuntimeError
697697

698-
data = np.arange(-10, 10, 1, dtype=float)
699-
data.shape = (10, 2)
698+
data = np.arange(-10, 10, 1, dtype=float).reshape((10, 2))
700699
mydata = data.view(MyArray)
701700

702701
for norm in [mcolors.Normalize(), mcolors.LogNorm(),

lib/mpl_toolkits/tests/test_axes_grid1.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,9 @@ def test_axes_locatable_position():
398398
savefig_kwarg={'bbox_inches': 'tight'})
399399
def test_image_grid():
400400
# test that image grid works with bbox_inches=tight.
401-
im = np.arange(100)
402-
im.shape = 10, 10
401+
im = np.arange(100).reshape((10, 10))
403402

404-
fig = plt.figure(1, (4., 4.))
403+
fig = plt.figure(1, (4, 4))
405404
grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=0.1)
406405

407406
for i in range(4):

0 commit comments

Comments
 (0)