Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commita730a69

Browse files
committed
Forbid categorical data on colorbars
1 parent2a60b36 commita730a69

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

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

Lines 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

Lines 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+
ifaxisisNone:
28+
raiseunits.ConversionError(
29+
"Categorical data does not support unit conversion that is not "
30+
"attached to an x or y Axis."
31+
)
32+
33+
2634
classStrCategoryConverter(units.ConversionInterface):
2735
@staticmethod
2836
defconvert(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
ifunitisNone:
4958
raiseValueError(
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
ifaxis.unitsisNone:
105116
axis.set_units(UnitData(data))

‎lib/matplotlib/cm.py

Lines 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
exceptExceptionase:
532+
ifisinstance(e,munits.ConversionError):
533+
raisee
534+
532535
raiseRuntimeError(
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
exceptExceptionase:
544+
ifisinstance(e,munits.ConversionError):
545+
raisee
546+
541547
raisemunits.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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
importmatplotlib.pyplotasplt
1010
importmatplotlib.categoryascat
1111
frommatplotlib.testing.decoratorsimportcheck_figures_equal
12+
importmatplotlib.unitsasmunits
1213

1314

1415
classTestUnitData:
@@ -321,3 +322,13 @@ def test_set_lim():
321322
ax.plot(["a","b","c","d"], [1,2,3,4])
322323
withwarnings.catch_warnings():
323324
ax.set_xlim("b","c")
325+
326+
327+
deftest_mappable_error():
328+
data=data= [["one","two"], ["three","four"]]
329+
330+
fig,ax=plt.subplots()
331+
withpytest.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)

[8]ページ先頭

©2009-2025 Movatter.jp