Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b17d79b

Browse filesBrowse files
committed
Allow ticks argument to take units
1 parent 96180e2 commit b17d79b
Copy full SHA for b17d79b

File tree

2 files changed

+9
-3
lines changed
Filter options

2 files changed

+9
-3
lines changed

‎lib/matplotlib/colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.py
+4-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_units.py
+5-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.