Skip to content

Allow turning off minor ticks on Colorbar with LogNorm #13265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down