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

Skip to content

Commit 7234bf5

Browse files
committed
Allow ticks argument to take units
1 parent f368afd commit 7234bf5

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
@@ -305,14 +305,18 @@ def test_mappable_units(quantity_converter):
305305
cbar = fig.colorbar(mappable, ax=ax, extend="min")
306306

307307
# pcolor
308+
#
308309
# Use datetime to check that the locator/formatter is set correctly
310+
# Also test ticks argument to colorbar
309311
data = [[datetime(2024, 1, 1), datetime(2024, 1, 2)],
310312
[datetime(2024, 1, 3), datetime(2024, 1, 4)]]
311313
vmin = datetime(2024, 1, 2)
312314
vmax = datetime(2024, 1, 5)
313315
ax = axs[0, 1]
314316
mappable = ax.pcolor(x, y, data, vmin=vmin, vmax=vmax)
315-
fig.colorbar(mappable, ax=ax, extend="min")
317+
318+
ticks = [datetime(2024, 1, 2), datetime(2024, 1, 3), datetime(2024, 1, 5)]
319+
fig.colorbar(mappable, ax=ax, extend="min", ticks=ticks)
316320

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

0 commit comments

Comments
 (0)