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

Skip to content

Commit 29ce2c2

Browse files
committed
FIX: fix check_1d to also check for ndim
Arrays sometimes don't have all the methods arrays should have, so add another check here. Plot requires both ndim and shape and this will extract the numpy array if x does not have those attributes. Otherwise leave the object alone, because unit support (currently only in plot) requires the object to retain the unit info.
1 parent f8cd2c9 commit 29ce2c2

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 6 additions & 1 deletion
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

Lines changed: 19 additions & 0 deletions
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)