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

Skip to content

Commit a7ba07c

Browse files
authored
Added get_shape as an alias for get_size + tests (#25425)
* Added get_shapes method as alias to get_size * merge tests and add exception test * match the correct runtime error * remove reduntant lines by making get_size call get_shape * simplify further * fix previous commit error
1 parent 5b1e02d commit a7ba07c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/matplotlib/image.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ def __init__(self, ax,
275275

276276
def __str__(self):
277277
try:
278-
size = self.get_size()
279-
return f"{type(self).__name__}(size={size!r})"
278+
shape = self.get_shape()
279+
return f"{type(self).__name__}(shape={shape!r})"
280280
except RuntimeError:
281281
return type(self).__name__
282282

@@ -286,10 +286,16 @@ def __getstate__(self):
286286

287287
def get_size(self):
288288
"""Return the size of the image as tuple (numrows, numcols)."""
289+
return self.get_shape()[:2]
290+
291+
def get_shape(self):
292+
"""
293+
Return the shape of the image as tuple (numrows, numcols, channels).
294+
"""
289295
if self._A is None:
290296
raise RuntimeError('You must first set the image array')
291297

292-
return self._A.shape[:2]
298+
return self._A.shape
293299

294300
def set_alpha(self, alpha):
295301
"""

lib/matplotlib/tests/test_image.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,3 +1468,15 @@ def test__resample_valid_output():
14681468
resample(np.zeros((9, 9), np.uint8), np.zeros((9, 9)))
14691469
with pytest.raises(ValueError, match="must be C-contiguous"):
14701470
resample(np.zeros((9, 9)), np.zeros((9, 9)).T)
1471+
1472+
1473+
def test_axesimage_get_shape():
1474+
# generate dummy image to test get_shape method
1475+
ax = plt.gca()
1476+
im = AxesImage(ax)
1477+
with pytest.raises(RuntimeError, match="You must first set the image array"):
1478+
im.get_shape()
1479+
z = np.arange(12, dtype=float).reshape((4, 3))
1480+
im.set_data(z)
1481+
assert im.get_shape() == (4, 3)
1482+
assert im.get_size() == im.get_shape()

0 commit comments

Comments
 (0)