diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 38bf57afdcb8..946692f62272 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -538,11 +538,7 @@ def update_ticks(self): long_axis.set_major_locator(locator) long_axis.set_major_formatter(formatter) if type(self.norm) == colors.LogNorm: - long_axis.set_minor_locator(_ColorbarLogLocator(self, - base=10., subs='auto')) - long_axis.set_minor_formatter( - ticker.LogFormatterSciNotation() - ) + self.minorticks_on() else: _log.debug('Using fixed locator on colorbar') ticks, ticklabels, offset_string = self._ticker(locator, formatter) @@ -602,6 +598,30 @@ def set_ticklabels(self, ticklabels, update_ticks=True): cbook._warn_external("set_ticks() must have been called.") self.stale = True + def minorticks_on(self): + """ + Turns on the minor ticks on the colorbar without extruding + into the "extend regions". + """ + ax = self.ax + long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis + + if long_axis.get_scale() == 'log': + long_axis.set_minor_locator(_ColorbarLogLocator(self, base=10., + subs='auto')) + long_axis.set_minor_formatter(ticker.LogFormatterSciNotation()) + else: + long_axis.set_minor_locator(_ColorbarAutoMinorLocator(self)) + + def minorticks_off(self): + """ + Turns off the minor ticks on the colorbar. + """ + ax = self.ax + long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis + + long_axis.set_minor_locator(ticker.NullLocator()) + def _config_axes(self, X, Y): ''' Make an axes patch and outline. @@ -1209,33 +1229,6 @@ def remove(self): # use_gridspec was True ax.set_subplotspec(subplotspec) - def minorticks_on(self): - """ - Turns on the minor ticks on the colorbar without extruding - into the "extend regions". - """ - ax = self.ax - long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis - - if long_axis.get_scale() == 'log': - cbook._warn_external('minorticks_on() has no effect on a ' - 'logarithmic colorbar axis') - else: - long_axis.set_minor_locator(_ColorbarAutoMinorLocator(self)) - - def minorticks_off(self): - """ - Turns off the minor ticks on the colorbar. - """ - ax = self.ax - long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis - - if long_axis.get_scale() == 'log': - cbook._warn_external('minorticks_off() has no effect on a ' - 'logarithmic colorbar axis') - else: - long_axis.set_minor_locator(ticker.NullLocator()) - @docstring.Substitution(make_axes_kw_doc) def make_axes(parents, location=None, orientation=None, fraction=0.15, diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 0769773252e4..fd1234221b78 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -307,6 +307,24 @@ def test_colorbar_minorticks_on_off(): np.testing.assert_almost_equal(cbar.ax.yaxis.get_minorticklocs(), correct_minorticklocs) + # tests for github issue #13257 and PR #13265 + data = np.random.uniform(low=1, high=10, size=(20, 20)) + + fig, ax = plt.subplots() + im = ax.pcolormesh(data, norm=LogNorm()) + cbar = fig.colorbar(im) + default_minorticklocks = cbar.ax.yaxis.get_minorticklocs() + + # test that minorticks turn off for LogNorm + cbar.minorticks_off() + assert np.array_equal(cbar.ax.yaxis.get_minorticklocs(), + np.array([])) + + # test that minorticks turn back on for LogNorm + cbar.minorticks_on() + assert np.array_equal(cbar.ax.yaxis.get_minorticklocs(), + default_minorticklocks) + def test_colorbar_autoticks(): # Test new autotick modes. Needs to be classic because