8000 Merge pull request #12656 from jklymak/f0x-colorbars-get_ticks-error · matplotlib/matplotlib@27f9f3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 27f9f3c

Browse files
authored
Merge pull request #12656 from jklymak/f0x-colorbars-get_ticks-error
FIX: fix error in colorbar.get_ticks not having valid data
2 parents 9fa3e60 + 15e4e79 commit 27f9f3c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ def __init__(self, ax, cmap=None,
386386
self.outline = None
387387
self.patch = None
388388
self.dividers = None
389+
self._manual_tick_data_values = None
389390

390391
if ticklocation == 'auto':
391392
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
@@ -573,7 +574,17 @@ def set_ticks(self, ticks, update_ticks=True):
573574

574575
def get_ticks(self, minor=False):
575576
"""Return the x ticks as a list of locations"""
576-
return self._tick_data_values
577+
if self._manual_tick_data_values is None:
578+
ax = self.ax
579+
if self.orientation == 'vertical':
580+
long_axis, short_axis = ax.yaxis, ax.xaxis
581+
else:
582+
long_axis, short_axis = ax.xaxis, ax.yaxis
583+
return long_axis.get_majorticklocs()
584+
else:
585+
# We made the axes manually, the old way, and the ylim is 0-1,
586+
# so the majorticklocs are in those units, not data units.
587+
return self._manual_tick_data_values
577588

578589
def set_ticklabels(self, ticklabels, update_ticks=True):
579590
"""
@@ -755,7 +766,7 @@ def _ticker(self, locator, formatter):
755766
else:
756767
eps = (intv[1] - intv[0]) * 1e-10
757768
b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)]
758-
self._tick_data_values = b
769+
self._manual_tick_data_values = b
759770
ticks = self._locate(b)
760771
formatter.set_locs(b)
761772
ticklabels = [formatter(t, i) for i, t in enumerate(b)]

lib/matplotlib/tests/test_colorbar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,13 @@ def test_colorbar_renorm():
436436
cbar.update_normal(im)
437437
assert np.isclose(cbar.vmin, z.min() * 1000)
438438
assert np.isclose(cbar.vmax, z.max() * 1000)
439+
440+
441+
def test_colorbar_get_ticks():
442+
with rc_context({'_internal.classic_mode': False}):
443+
444+
fig, ax = plt. subplots()
445+
np.random.seed(19680801)
446+
pc = ax.pcolormesh(np.random.rand(30, 30))
447+
cb = fig.colorbar(pc)
448+
np.testing.assert_allclose(cb.get_ticks(), [0.2, 0.4, 0.6, 0.8])

0 commit comments

Comments
 (0)
0