8000 Add is_natively_supported in ConversionInterface · matplotlib/matplotlib@4005002 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4005002

Browse files
committed
Add is_natively_supported in ConversionInterface
1 parent 7d13788 commit 4005002

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/matplotlib/axis.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,10 @@ def have_units(self):
15651565
return self.converter is not None or self.units is not None
15661566

15671567
def convert_units(self, x):
1568+
# If x is natively supported by Matplotlib, doesn't need converting
1569+
if munits.ConversionInterface.is_natively_supported(x):
1570+
return x
1571+
15681572
if self.converter is None:
15691573
self.converter = munits.registry.get_converter(x)
15701574

@@ -1573,13 +1577,8 @@ def convert_units(self, x):
15731577
try:
15741578
ret = self.converter.convert(x, self.units, self)
15751579
except Exception as e:
1576-
# If x is already number like, converters other than
1577-
# DecimalConverter may raise error, so check here to walk around.
1578-
if munits.ConversionInterface.is_numlike(x):
1579-
return x
1580-
# Otherwise, the conversion fails
1581-
raise munits.ConversionError('Failed to convert value(s) to '
1582-
f'axis units: {x!r}') from e
1580+
raise munits.ConversionError('Failed to convert value(s) to axis '
1581+
f'units: {x!r}') from e
15831582
return ret
15841583

15851584
def set_units(self, u):

lib/matplotlib/units.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ def is_numlike(x):
134134
else:
135135
return isinstance(x, Number)
136136

137+
@staticmethod
138+
def is_natively_supported(x):
139+
"""
140+
Return whether *x* is of a type that Matplotlib natively supports or
141+
*x* is array of objects of such types.
142+
"""
143+
# Matplotlib natively supports all number types except Decimal
144+
if np.iterable(x):
145+
# Assume lists are homogeneous as other functions in unit system
146+
for thisx in x:
147+
return (isinstance(thisx, Number) and
148+
not isinstance(thisx, Decimal))
149+
else:
150+
return isinstance(x, Number) and not isinstance(x, Decimal)
151+
137152

138153
class DecimalConverter(ConversionInterface):
139154
"""

0 commit comments

Comments
 (0)
0