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

Skip to content

Commit 5d15e7c

Browse files
author
Nathan Goldbaum
committed
Ensure image scale factors are scalars
This avoids errors that get triggered by passing an ndarray subclass to imshow. For example: ```python import numpy as np from yt.units import km from matplotlib import pyplot as plt data = np.random.random((100, 100))*km plt.imshow(data) plt.show() ``` Or similarly with `astropy`: ```python import numpy as np from astropy.units import km from matplotlib import pyplot as plt data = np.random.random((100, 100))*km plt.imshow(data) plt.show() ``` Before this patch, both of these commands raise an error because `a_min` and `a_max` have units attached. By explicitly converting these to scalars we avoid this failure mode.
1 parent 4b5f6e3 commit 5d15e7c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/matplotlib/image.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
394394
A_scaled = np.empty(A.shape, dtype=scaled_dtype)
395395
A_scaled[:] = A
396396
A_scaled -= a_min
397-
a_min = a_min.astype(scaled_dtype)
398-
a_max = a_max.astype(scaled_dtype)
397+
# a_min and a_max might be ndarray subclasses so use
398+
# asscalar to ensure they are scalars to avoid errors
399+
a_min = np.asscalar(a_min.astype(scaled_dtype))
400+
a_max = np.asscalar(a_max.astype(scaled_dtype))
399401

400402
if a_min != a_max:
401403
A_scaled /= ((a_max - a_min) / 0.8)

0 commit comments

Comments
 (0)