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

Skip to content

Commit 387c345

Browse files
committed
Improve error messages for unit conversion
1 parent 739b86a commit 387c345

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
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+
'units: %r' % x) from e
14911494
return ret
14921495

14931496
def set_units(self, u):

lib/matplotlib/category.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@ def convert(value, unit, axis):
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 unit for StrCategoryConverter. This might be caused'
47+
'by unintendedly mixing categorical and numeric data.')
48+
4449
# dtype = object preserves numerical pass throughs
4550
values = np.atleast_1d(np.array(value, dtype=object))
4651

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(ValueError):
53+
pass
54+
55+
5256
class AxisInfo(object):
5357
"""
5458
Information to support default axis labeling, tick labeling, and

0 commit comments

Comments
 (0)