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

Skip to content

Commit c9a0ac7

Browse files
committed
Improve error messages for unit conversion
1 parent de38ef2 commit c9a0ac7

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

lib/matplotlib/axis.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,8 +1486,11 @@ def convert_units(self, x):
14861486

14871487
if self.converter is None:
14881488
return x
1489-
1490-
ret = self.converter.convert(x, self.units, self)
1489+
try:
1490+
ret = self.converter.convert(x, self.units, self)
1491+
except Exception as e:
1492+
raise munits.ConversionError('Failed to convert value(s) to axis '
1493+
f'units: {x!r}') from e
14911494
return ret
14921495

14931496
def set_units(self, u):

lib/matplotlib/category.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,30 @@
2323
class StrCategoryConverter(units.ConversionInterface):
2424
@staticmethod
2525
def convert(value, unit, axis):
26-
"""Converts strings in value to floats using
26+
"""Convert strings in value to floats using
2727
mapping information store in the unit object.
2828
2929
Parameters
3030
----------
3131
value : string or iterable
32-
value or list of values to be converted
33-
unit : :class:`.UnitData`
34-
object string unit information for value
35-
axis : :class:`~matplotlib.Axis.axis`
36-
axis on which the converted value is plotted
32+
Value or list of values to be converted.
33+
unit : `.UnitData`
34+
An object mapping strings to integers.
35+
axis : `~matplotlib.axis.Axis`
36+
axis on which the converted value is plotted.
37+
38+
.. note:: *axis* is unused.
3739
3840
Returns
3941
-------
40-
mapped_ value : float or ndarray[float]
41-
42-
.. note:: axis is not used in this function
42+
mapped_value : float or ndarray[float]
4343
"""
44+
if unit is None:
45+
raise ValueError(
46+
'Missing category information for StrCategoryConverter; '
47+
'this might be caused by unintendedly mixing categorical and '
48+
'numeric data')
49+
4450
# dtype = object preserves numerical pass throughs
4551
values = np.atleast_1d(np.array(value, dtype=object))
4652

@@ -190,7 +196,7 @@ def update(self, data):
190196
self._mapping[val] = next(self._counter)
191197

192198

193-
# Connects the convertor to matplotlib
199+
# Register the converter with Matplotlib's unit framework
194200
units.registry[str] = StrCategoryConverter()
195201
units.registry[np.str_] = StrCategoryConverter()
196202
units.registry[bytes] = StrCategoryConverter()

lib/matplotlib/units.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def default_units(x, axis):
4949
from matplotlib import cbook
5050

5151

52+
class ConversionError(TypeError):
53+
pass
54+
55+
5256
class AxisInfo(object):
5357
"""
5458
Information to support default axis labeling, tick labeling, and

0 commit comments

Comments
 (0)