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

Skip to content

Commit f368afd

Browse files
committed
Add support for categorical colorbars
1 parent 726547e commit f368afd

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-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
@@ -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

Lines 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)

lib/matplotlib/tests/test_units.py

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

317-
# pcolormesh + horizontal colorbar
317+
# pcolormesh + horizontal colorbar + categorical
318+
data = [["one", "two"], ["three", "four"]]
318319
ax = axs[1, 0]
319-
mappable = ax.pcolormesh(x, y, data, vmin=vmin, vmax=vmax)
320+
mappable = ax.pcolormesh(x, y, data)
320321
fig.colorbar(mappable, ax=ax, orientation="horizontal", extend="min")
321322

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

0 commit comments

Comments
 (0)