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

Skip to content

Commit 5f8e365

Browse files
committed
Allow ticks argument to take units
1 parent 8730c8a commit 5f8e365

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ def __init__(self, ax, mappable=None, *, cmap=None,
370370
self.solids_patches = []
371371
self.lines = []
372372

373-
374373
for spine in self.ax.spines.values():
375374
spine.set_visible(False)
376375
self.outline = self.ax.spines['outline'] = _ColorbarSpine(self.ax)
@@ -397,9 +396,12 @@ def __init__(self, ax, mappable=None, *, cmap=None,
397396
self._reset_locator_formatter_scale()
398397
self._set_units_from_mappable()
399398

399+
if ticks is not None and self._converter is not None:
400+
ticks = self._converter.convert(ticks, self._units, self._long_axis())
401+
400402
if np.iterable(ticks):
401403
self._locator = ticker.FixedLocator(ticks, nbins=len(ticks))
402-
else:
404+
elif isinstance(ticks, ticker.Locator):
403405
self._locator = ticks
404406

405407
if isinstance(format, str):

lib/matplotlib/tests/test_units.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,18 @@ def test_mappable_units(quantity_converter):
316316
cbar = fig.colorbar(mappable, ax=ax, extend="min")
317317

318318
# pcolor
319+
#
319320
# Use datetime to check that the locator/formatter is set correctly
321+
# Also test ticks argument to colorbar
320322
data = [[datetime(2024, 1, 1), datetime(2024, 1, 2)],
321323
[datetime(2024, 1, 3), datetime(2024, 1, 4)]]
322324
vmin = datetime(2024, 1, 2)
323325
vmax = datetime(2024, 1, 5)
324326
ax = axs[0, 1]
325327
mappable = ax.pcolor(x, y, data, vmin=vmin, vmax=vmax)
326-
fig.colorbar(mappable, ax=ax, extend="min")
328+
329+
ticks = [datetime(2024, 1, 2), datetime(2024, 1, 3), datetime(2024, 1, 5)]
330+
fig.colorbar(mappable, ax=ax, extend="min", ticks=ticks)
327331

328332
# pcolormesh + horizontal colorbar + categorical
329333
data = [["one", "two"], ["three", "four"]]

0 commit comments

Comments
 (0)