Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 96180e2

Browse filesBrowse files
committed
Forbid categorical data on colorbars
1 parent 99b8061 commit 96180e2
Copy full SHA for 96180e2

File tree

4 files changed

+32
-0
lines changed
Filter options

4 files changed

+32
-0
lines changed

‎doc/api/next_api_changes/behavior/xxxxx-DS.rst

Copy file name to clipboardExpand all lines: doc/api/next_api_changes/behavior/xxxxx-DS.rst
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ convert data of more than one dimension (e.g. when plotting images the data is 2
1414
If a conversion interface raises an error when given ``None`` or 2D data as described
1515
above, this error will be re-raised when a user tries to use one of the newly supported
1616
plotting methods with unit-ful data.
17+
18+
If you have a custom conversion interface you want to forbid using with image data, the
19+
`~.units.ConversionInterface` methods that accept a ``units`` parameter should raise
20+
a `matplotlib.units.ConversionError` when given ``units=None``.

‎lib/matplotlib/category.py

Copy file name to clipboardExpand all lines: lib/matplotlib/category.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
_log = logging.getLogger(__name__)
2424

2525

26+
def _check_axis(axis):
27+
if axis is None:
28+
raise units.ConversionError(
29+
"Categorical data does not support unit conversion that is not "
30+
"attached to an x or y Axis."
31+
)
32+
33+
2634
class StrCategoryConverter(units.ConversionInterface):
2735
@staticmethod
2836
def convert(value, unit, axis):
@@ -45,6 +53,7 @@ def convert(value, unit, axis):
4553
-------
4654
float or `~numpy.ndarray` of float
4755
"""
56+
_check_axis(axis)
4857
if unit is None:
4958
raise ValueError(
5059
'Missing category information for StrCategoryConverter; '
@@ -77,6 +86,7 @@ def axisinfo(unit, axis):
7786
Information to support default tick labeling
7887
7988
"""
89+
_check_axis(axis)
8090
StrCategoryConverter._validate_unit(unit)
8191
# locator and formatter take mapping dict because
8292
# args need to be pass by reference for updates
@@ -100,6 +110,7 @@ def default_units(data, axis):
100110
`.UnitData`
101111
object storing string to integer mapping
102112
"""
113+
_check_axis(axis)
103114
# the conversion call stack is default_units -> axis_info -> convert
104115
if axis.units is None:
105116
axis.set_units(UnitData(data))

‎lib/matplotlib/cm.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cm.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ def _strip_units(self, A):
529529
try:
530530
self._units = self._converter.default_units(A, None)
531531
except Exception as e:
532+
if isinstance(e, munits.ConversionError):
533+
raise e
534+
532535
raise RuntimeError(
533536
f'{self._converter} failed when trying to return the default units for '
534537
'this image. This may be because support has not been '
@@ -538,6 +541,9 @@ def _strip_units(self, A):
538541
try:
539542
A = self._converter.convert(A, self._units, None)
540543
except Exception as e:
544+
if isinstance(e, munits.ConversionError):
545+
raise e
546+
541547
raise munits.ConversionError(
542548
f'{self._converter} failed when trying to convert the units for this '
543549
'image. This may be because support has not been implemented '

‎lib/matplotlib/tests/test_category.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_category.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import matplotlib.pyplot as plt
1010
import matplotlib.category as cat
1111
from matplotlib.testing.decorators import check_figures_equal
12+
import matplotlib.units as munits
1213

1314

1415
class TestUnitData:
@@ -321,3 +322,13 @@ def test_set_lim():
321322
ax.plot(["a", "b", "c", "d"], [1, 2, 3, 4])
322323
with warnings.catch_warnings():
323324
ax.set_xlim("b", "c")
325+
326+
327+
def test_mappable_error():
328+
data = data = [["one", "two"], ["three", "four"]]
329+
330+
fig, ax = plt.subplots()
331+
with pytest.raises(munits.ConversionError,
332+
match=('Categorical data does not support unit conversion '
333+
'that is not attached to an x or y Axis.')):
334+
ax.imshow(data)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.