8000 Merge pull request #12124 from dstansby/empty-units · matplotlib/matplotlib@d0e408e · GitHub
[go: up one dir, main page]

Skip to content

Commit d0e408e

Browse files
authored
Merge pull request #12124 from dstansby/empty-units
FIX: Correctly infer units from empty arrays
2 parents 68314b0 + d00127e commit d0e408e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/matplotlib/tests/test_units.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,8 @@ def test_jpl_barh_units():
127127
fig, ax = plt.subplots()
128128
ax.barh(x, w, left=b)
129129
ax.set_xlim([b-1*day, b+w[-1]+1*day])
130+
131+
132+
def test_emtpy_arrays():
133+
# Check that plotting an empty array with a dtype works
134+
plt.scatter(np.array([], dtype='datetime64[ns]'), np.array([]))

lib/matplotlib/units.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,16 @@ def get_converter(self, x):
160160
x = x.values
161161

162162
# If x is an array, look inside the array for data with units
163-
if isinstance(x, np.ndarray) and x.size:
163+
if isinstance(x, np.ndarray):
164+
# If there are no elements in x, infer the units from its dtype
165+
if not x.size:
166+
return self.get_converter(np.array([0], dtype=x.dtype))
164167
xravel = x.ravel()
165168
try:
166169
# pass the first value of x that is not masked back to
167170
# get_converter
168171
if not np.all(xravel.mask):
169-
# some elements are not masked
172+
# Get first non-masked item
170173
converter = self.get_converter(
171174
xravel[np.argmin(xravel.mask)])
172175
return converter

0 commit comments

Comments
 (0)
0