Skip to content

Commit 99b7695

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #22975: MNT: fix __array__ to numpy
1 parent 59d9eda commit 99b7695

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
@@ -1302,7 +1302,12 @@ def _check_1d(x):
13021302
"""Convert scalars to 1D arrays; pass-through arrays as is."""
13031303
# Unpack in case of e.g. Pandas or xarray object
13041304
x = _unpack_to_numpy(x)
1305-
if not hasattr(x, 'shape') or len(x.shape) < 1:
1305+
# plot requires `shape` and `ndim`. If passed an
1306+
# object that doesn't provide them, then force to numpy array.
1307+
# Note this will strip unit information.
1308+
if (not hasattr(x, 'shape') or
1309+
not hasattr(x, 'ndim') or
1310+
len(x.shape) < 1):
13061311
return np.atleast_1d(x)
13071312
else:
13081313
return x

lib/matplotlib/tests/test_units.py

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

0 commit comments

Comments
 (0)