diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index ad6a145a9d1..69dd6ec4346 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -341,6 +341,7 @@ def draw_all(self): Calculate any free parameters based on the current cmap and norm, and do all the drawing. ''' + self._process_values() self._find_range() X, Y = self._mesh() @@ -396,6 +397,10 @@ def set_ticks(self, ticks, update_ticks=True): self.update_ticks() self.stale = True + def get_ticks(self, minor=False): + """Return the x ticks as a list of locations""" + return self._tick_data_values + def set_ticklabels(self, ticklabels, update_ticks=True): """ set tick labels. Tick labels are updated immediately unless @@ -598,6 +603,7 @@ def _ticker(self): else: eps = (intv[1] - intv[0]) * 1e-10 b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)] + self._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 8d0bc1d709b..d142fbc51e9 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -298,6 +298,33 @@ def test_colorbar_ticks(): assert len(cbar.ax.xaxis.get_ticklocs()) == len(clevs) +@cleanup +def test_colorbar_get_ticks(): + # test feature for #5792 + plt.figure() + data = np.arange(1200).reshape(30, 40) + levels = [0, 200, 400, 600, 800, 1000, 1200] + + plt.subplot() + plt.contourf(data, levels=levels) + + # testing getter for user set ticks + userTicks = plt.colorbar(ticks=[0, 600, 1200]) + assert userTicks.get_ticks().tolist() == [0, 600, 1200] + + # testing for getter after calling set_ticks + userTicks.set_ticks([600, 700, 800]) + assert userTicks.get_ticks().tolist() == [600, 700, 800] + + # testing for getter after calling set_ticks with some ticks out of bounds + userTicks.set_ticks([600, 1300, 1400, 1500]) + assert userTicks.get_ticks().tolist() == [600] + + # testing getter when no ticks are assigned + defTicks = plt.colorbar(orientation='horizontal') + assert defTicks.get_ticks().tolist() == levels + + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False)