From 97209bdf9d3c76105249523a4072e3114975ff8f Mon Sep 17 00:00:00 2001 From: LevN0 Date: Tue, 22 Jan 2019 18:44:28 -0500 Subject: [PATCH 1/4] Allow turning off minor ticks on Colorbar with LogNorm Allow turning off minor ticks on Colorbar with LogNorm --- lib/matplotlib/colorbar.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 38bf57afdcb8..31c7dd094dfb 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1230,11 +1230,7 @@ def minorticks_off(self): 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()) + long_axis.set_minor_locator(ticker.NullLocator()) @docstring.Substitution(make_axes_kw_doc) From 52e802b824935347744872be349cc6c60c07fc4e Mon Sep 17 00:00:00 2001 From: LevN0 Date: Tue, 22 Jan 2019 20:22:51 -0500 Subject: [PATCH 2/4] Also allow minorticks_on for LogNorm Also allow minorticks_on for LogNorm --- lib/matplotlib/colorbar.py | 53 ++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 31c7dd094dfb..169eb3e2e85d 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,29 +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 - - long_axis.set_minor_locator(ticker.NullLocator()) - @docstring.Substitution(make_axes_kw_doc) def make_axes(parents, location=None, orientation=None, fraction=0.15, From 3dca9fd1d94591d0f149c883a4da0ab5fc25009f Mon Sep 17 00:00:00 2001 From: LevN0 Date: Tue, 22 Jan 2019 20:52:40 -0500 Subject: [PATCH 3/4] PEP8 PEP8 --- lib/matplotlib/colorbar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 169eb3e2e85d..946692f62272 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -607,8 +607,8 @@ def minorticks_on(self): 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_locator(_ColorbarLogLocator(self, base=10., + subs='auto')) long_axis.set_minor_formatter(ticker.LogFormatterSciNotation()) else: long_axis.set_minor_locator(_ColorbarAutoMinorLocator(self)) From eb411d0e3a6dcd3ece27c3643ed0bd3fb9102230 Mon Sep 17 00:00:00 2001 From: LevN0 Date: Tue, 22 Jan 2019 23:59:01 -0500 Subject: [PATCH 4/4] Add unit tests Add unit tests --- lib/matplotlib/tests/test_colorbar.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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