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

Skip to content

Commit ffdc34c

Browse files
committed
Improve categorical converter error message
1 parent ff54be0 commit ffdc34c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lib/matplotlib/category.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def convert(value, unit, axis):
5050
'Missing category information for StrCategoryConverter; '
5151
'this might be caused by unintendedly mixing categorical and '
5252
'numeric data')
53+
StrCategoryConverter._validate_unit(unit)
5354
# dtype = object preserves numerical pass throughs
5455
values = np.atleast_1d(np.array(value, dtype=object))
5556
# pass through sequence of non binary numbers
@@ -80,6 +81,7 @@ def axisinfo(unit, axis):
8081
8182
.. note: axis is not used
8283
"""
84+
StrCategoryConverter._validate_unit(unit)
8385
# locator and formatter take mapping dict because
8486
# args need to be pass by reference for updates
8587
majloc = StrCategoryLocator(unit._mapping)
@@ -109,6 +111,13 @@ def default_units(data, axis):
109111
axis.units.update(data)
110112
return axis.units
111113

114+
@staticmethod
115+
def _validate_unit(unit):
116+
if not hasattr(unit, '_mapping'):
117+
raise ValueError(
118+
f'Provided unit "{unit}" is not a valid for a categorical '
119+
'converter, as it does not have a _mapping attribute.')
120+
112121

113122
class StrCategoryLocator(ticker.Locator):
114123
"""Tick at every integer mapping of the string data."""

0 commit comments

Comments
 (0)