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

Skip to content

Commit 8730c8a

Browse files
committed
Add support for categorical colorbars
1 parent ff5d001 commit 8730c8a

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
@@ -402,6 +402,9 @@ def _strip_units(self, A):
402402
try:
403403
self._units = self._converter.default_units(A, None)
404404
except Exception as e:
405+
if isinstance(e, munits.ConversionError):
406+
raise e
407+
405408
raise RuntimeError(
406409
f'{self._converter} failed when trying to return the default units for '
407410
'this image. This may be because support has not been '
@@ -411,6 +414,9 @@ def _strip_units(self, A):
411414
try:
412415
A = self._converter.convert(A, self._units, None)
413416
except Exception as e:
417+
if isinstance(e, munits.ConversionError):
418+
raise e
419+
414420
raise munits.ConversionError(
415421
f'{self._converter} failed when trying to convert the units for this '
416422
'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
@@ -325,9 +325,10 @@ def test_mappable_units(quantity_converter):
325325
mappable = ax.pcolor(x, y, data, vmin=vmin, vmax=vmax)
326326
fig.colorbar(mappable, ax=ax, extend="min")
327327

328-
# pcolormesh + horizontal colorbar
328+
# pcolormesh + horizontal colorbar + categorical
329+
data = [["one", "two"], ["three", "four"]]
329330
ax = axs[1, 0]
330-
mappable = ax.pcolormesh(x, y, data, vmin=vmin, vmax=vmax)
331+
mappable = ax.pcolormesh(x, y, data)
331332
fig.colorbar(mappable, ax=ax, orientation="horizontal", extend="min")
332333

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

0 commit comments

Comments
 (0)