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([])) 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