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

Skip to content

Backport PR #13005 on branch v3.0.x (Improve error messages for unit conversion) #13168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1526,8 +1526,11 @@ def convert_units(self, x):

if self.converter is None:
return x

ret = self.converter.convert(x, self.units, self)
try:
ret = self.converter.convert(x, self.units, self)
except Exception as e:
raise munits.ConversionError('Failed to convert value(s) to axis '
f'units: {x!r}') from e
return ret

def set_units(self, u):
Expand Down
26 changes: 16 additions & 10 deletions lib/matplotlib/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,30 @@
class StrCategoryConverter(units.ConversionInterface):
@staticmethod
def convert(value, unit, axis):
"""Converts strings in value to floats using
"""Convert strings in value to floats using
mapping information store in the unit object.

Parameters
----------
value : string or iterable
value or list of values to be converted
unit : :class:`.UnitData`
object string unit information for value
axis : :class:`~matplotlib.Axis.axis`
axis on which the converted value is plotted
Value or list of values to be converted.
unit : `.UnitData`
An object mapping strings to integers.
axis : `~matplotlib.axis.Axis`
axis on which the converted value is plotted.

.. note:: *axis* is unused.

Returns
-------
mapped_ value : float or ndarray[float]

.. note:: axis is not used in this function
mapped_value : float or ndarray[float]
"""
if unit is None:
raise ValueError(
'Missing category information for StrCategoryConverter; '
'this might be caused by unintendedly mixing categorical and '
'numeric data')

# dtype = object preserves numerical pass throughs
values = np.atleast_1d(np.array(value, dtype=object))

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


# Connects the convertor to matplotlib
# Register the converter with Matplotlib's unit framework
units.registry[str] = StrCategoryConverter()
units.registry[np.str_] = StrCategoryConverter()
units.registry[bytes] = StrCategoryConverter()
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def default_units(x, axis):
from matplotlib.cbook import iterable, safe_first_element


class ConversionError(TypeError):
pass


class AxisInfo(object):
"""
Information to support default axis labeling, tick labeling, and
Expand Down