8000 Allow ticks argument to take units · matplotlib/matplotlib@5591a33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5591a33

Browse files
committed
Allow ticks argument to take units
1 parent a730a69 commit 5591a33

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
@@ -371,7 +371,6 @@ def __init__(self, ax, mappable=None, *, cmap=None,
371371
self.solids_patches = []
372372
self.lines = []
373373

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

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

406408
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
318322
ax = axs[1, 0]

0 commit comments

Comments
 (0)
0