Skip to content

Commit da84e38

Browse files
authored
Merge pull request #22975 from jklymak/mnt-fix-array-numpy-second-try
MNT: fix __array__ to numpy
2 parents e1bdb3b + 29ce2c2 commit da84e38

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/matplotlib/cbook/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,12 @@ def _check_1d(x):
13331333
"""Convert scalars to 1D arrays; pass-through arrays as is."""
13341334
# Unpack in case of e.g. Pandas or xarray object
13351335
x = _unpack_to_numpy(x)
1336-
if not hasattr(x, 'shape') or len(x.shape) < 1:
1336+
# plot requires `shape` and `ndim`. If passed an
1337+
# object that doesn't provide them, then force to numpy array.
1338+
# Note this will strip unit information.
1339+
if (not hasattr(x, 'shape') or
1340+
not hasattr(x, 'ndim') or
1341+
len(x.shape) < 1):
13371342
return np.atleast_1d(x)
13381343
else:
13391344
return x

lib/matplotlib/tests/test_units.py

+19
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,22 @@ def test_empty_default_limits(quantity_converter):
264264
fig.draw_without_rendering()
265265
assert ax.get_ylim() == (0, 100)
266266
assert ax.get_xlim() == (28.5, 31.5)
267+
268+
269+
# test array-like objects...
270+
class Kernel:
271+
def __init__(self, array):
272+
self._array = np.asanyarray(array)
273+
274+
def __array__(self):
275+
return self._array
276+
277+
@property
278+
def shape(self):
279+
return self._array.shape
280+
281+
282+
def test_plot_kernel():
283+
# just a smoketest that fail
284+
kernel = Kernel([1, 2, 3, 4, 5])
285+
plt.plot(kernel)

0 commit comments

Comments
 (0)