diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 6eeeb31628ab..cd06617ad140 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -386,6 +386,7 @@ def __init__(self, ax, cmap=None, self.outline = None self.patch = None self.dividers = None + self._manual_tick_data_values = None if ticklocation == 'auto': ticklocation = 'bottom' if orientation == 'horizontal' else 'right' @@ -573,7 +574,17 @@ def set_ticks(self, ticks, update_ticks=True): def get_ticks(self, minor=False): """Return the x ticks as a list of locations""" - return self._tick_data_values + if self._manual_tick_data_values is None: + ax = self.ax + if self.orientation == 'vertical': + long_axis, short_axis = ax.yaxis, ax.xaxis + else: + long_axis, short_axis = ax.xaxis, ax.yaxis + return long_axis.get_majorticklocs() + else: + # We made the axes manually, the old way, and the ylim is 0-1, + # so the majorticklocs are in those units, not data units. + return self._manual_tick_data_values def set_ticklabels(self, ticklabels, update_ticks=True): """ @@ -755,7 +766,7 @@ def _ticker(self, locator, formatter): else: eps = (intv[1] - intv[0]) * 1e-10 b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)] - self._tick_data_values = b + self._manual_tick_data_values = b ticks = self._locate(b) formatter.set_locs(b) ticklabels = [formatter(t, i) for i, t in enumerate(b)] diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 6137da4afdba..1c358c09ae78 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -436,3 +436,13 @@ def test_colorbar_renorm(): cbar.update_normal(im) assert np.isclose(cbar.vmin, z.min() * 1000) assert np.isclose(cbar.vmax, z.max() * 1000) + + +def test_colorbar_get_ticks(): + with rc_context({'_internal.classic_mode': False}): + + fig, ax = plt. subplots() + np.random.seed(19680801) + pc = ax.pcolormesh(np.random.rand(30, 30)) + cb = fig.colorbar(pc) + np.testing.assert_allclose(cb.get_ticks(), [0.2, 0.4, 0.6, 0.8])