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

Skip to content

Commit 979b099

Browse files
committed
Add support for categorical colorbars
1 parent 084850b commit 979b099

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

doc/api/next_api_changes/behavior/27721-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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def default_units(data, axis):
101101
object storing string to integer mapping
102102
"""
103103
# the conversion call stack is default_units -> axis_info -> convert
104+
if axis is None:
105+
return UnitData(data)
104106
if axis.units is None:
105107
axis.set_units(UnitData(data))
106108
else:
@@ -208,7 +210,7 @@ def update(self, data):
208210
TypeError
209211
If elements in *data* are neither str nor bytes.
210212
"""
211-
data = np.atleast_1d(np.array(data, dtype=object))
213+
data = np.atleast_1d(np.array(data, dtype=object).ravel())
212214
# check if convertible to number:
213215
convertible = True
214216
for val in OrderedDict.fromkeys(data):

lib/matplotlib/cm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ def _strip_units(self, A):
408408
try:
409409
self._units = self._converter.default_units(A, None)
410410
except Exception as e:
411+
if isinstance(e, munits.ConversionError):
412+
raise e
413+
411414
raise RuntimeError(
412415
f'{self._converter} failed when trying to return the default units for '
413416
'this image. This may be because support has not been '
@@ -417,6 +420,9 @@ def _strip_units(self, A):
417420
try:
418421
A = self._converter.convert(A, self._units, None)
419422
except Exception as e:
423+
if isinstance(e, munits.ConversionError):
424+
raise e
425+
420426
raise munits.ConversionError(
421427
f'{self._converter} failed when trying to convert the units for this '
422428
'image. This may be because support has not been implemented '

lib/matplotlib/tests/test_units.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,10 @@ def test_mappable_units(quantity_converter):
333333
mappable = ax.pcolor(x, y, data, vmin=vmin, vmax=vmax)
334334
fig.colorbar(mappable, ax=ax, extend="min")
335335

336-
# pcolormesh + horizontal colorbar
336+
# pcolormesh + horizontal colorbar + categorical
337+
data = [["one", "two"], ["three", "four"]]
337338
ax = axs[1, 0]
338-
mappable = ax.pcolormesh(x, y, data, vmin=vmin, vmax=vmax)
339+
mappable = ax.pcolormesh(x, y, data)
339340
fig.colorbar(mappable, ax=ax, orientation="horizontal", extend="min")
340341

341342
axs[1, 1].axis("off")

0 commit comments

Comments
 (0)