From c250254210f48d65009bb066d4487452b63dbf05 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 15 Sep 2018 12:23:34 +0100 Subject: [PATCH 1/2] Fix inferring units for zero-sized arrays --- lib/matplotlib/units.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/units.py b/lib/matplotlib/units.py index 12d7a7e2617e..adfa771d4190 100644 --- a/lib/matplotlib/units.py +++ b/lib/matplotlib/units.py @@ -160,13 +160,16 @@ def get_converter(self, x): x = x.values # If x is an array, look inside the array for data with units - if isinstance(x, np.ndarray) and x.size: + if isinstance(x, np.ndarray): + # If there are no elements in x, infer the units from its dtype + if not x.size: + return self.get_converter(np.array([0], dtype=x.dtype)) xravel = x.ravel() try: # pass the first value of x that is not masked back to # get_converter if not np.all(xravel.mask): - # some elements are not masked + # Get first non-masked item converter = self.get_converter( xravel[np.argmin(xravel.mask)]) return converter From d00127e8ddfa44becd51e1e83e38bd1e7136fd52 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 15 Sep 2018 12:23:40 +0100 Subject: [PATCH 2/2] Add test --- lib/matplotlib/tests/test_units.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/matplotlib/tests/test_units.py b/lib/matplotlib/tests/test_units.py index 4a6d1e87c89f..6c677b4b2557 100644 --- a/lib/matplotlib/tests/test_units.py +++ b/lib/matplotlib/tests/test_units.py @@ -127,3 +127,8 @@ def test_jpl_barh_units(): fig, ax = plt.subplots() ax.barh(x, w, left=b) ax.set_xlim([b-1*day, b+w[-1]+1*day]) + + +def test_emtpy_arrays(): + # Check that plotting an empty array with a dtype works + plt.scatter(np.array([], dtype='datetime64[ns]'), np.array([]))