Skip to content

Commit 397e79e

Browse files
authored
Merge pull request #13265 from LevN0/allow-disable-minorticks-lognorm-colorbar
Allow turning off minor ticks on Colorbar with LogNorm
2 parents 2660901 + eb411d0 commit 397e79e

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

lib/matplotlib/colorbar.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,7 @@ def update_ticks(self):
538538
long_axis.set_major_locator(locator)
539539
long_axis.set_major_formatter(formatter)
540540
if type(self.norm) == colors.LogNorm:
541-
long_axis.set_minor_locator(_ColorbarLogLocator(self,
542-
base=10., subs='auto'))
543-
long_axis.set_minor_formatter(
544-
ticker.LogFormatterSciNotation()
545-
)
541+
self.minorticks_on()
546542
else:
547543
_log.debug('Using fixed locator on colorbar')
548544
ticks, ticklabels, offset_string = self._ticker(locator, formatter)
@@ -602,6 +598,30 @@ def set_ticklabels(self, ticklabels, update_ticks=True):
602598
cbook._warn_external("set_ticks() must have been called.")
603599
self.stale = True
604600

601+
def minorticks_on(self):
602+
"""
603+
Turns on the minor ticks on the colorbar without extruding
604+
into the "extend regions".
605+
"""
606+
ax = self.ax
607+
long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis
608+
609+
if long_axis.get_scale() == 'log':
610+
long_axis.set_minor_locator(_ColorbarLogLocator(self, base=10.,
611+
subs='auto'))
612+
long_axis.set_minor_formatter(ticker.LogFormatterSciNotation())
613+
else:
614+
long_axis.set_minor_locator(_ColorbarAutoMinorLocator(self))
615+
616+
def minorticks_off(self):
617+
"""
618+
Turns off the minor ticks on the colorbar.
619+
"""
620+
ax = self.ax
621+
long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis
622+
623+
long_axis.set_minor_locator(ticker.NullLocator())
624+
605625
def _config_axes(self, X, Y):
606626
'''
607627
Make an axes patch and outline.
@@ -1209,33 +1229,6 @@ def remove(self):
12091229
# use_gridspec was True
12101230
ax.set_subplotspec(subplotspec)
12111231

1212-
def minorticks_on(self):
1213-
"""
1214-
Turns on the minor ticks on the colorbar without extruding
1215-
into the "extend regions".
1216-
"""
1217-
ax = self.ax
1218-
long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis
1219-
1220-
if long_axis.get_scale() == 'log':
1221-
cbook._warn_external('minorticks_on() has no effect on a '
1222-
'logarithmic colorbar axis')
1223-
else:
1224-
long_axis.set_minor_locator(_ColorbarAutoMinorLocator(self))
1225-
1226-
def minorticks_off(self):
1227-
"""
1228-
Turns off the minor ticks on the colorbar.
1229-
"""
1230-
ax = self.ax
1231-
long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis
1232-
1233-
if long_axis.get_scale() == 'log':
1234-
cbook._warn_external('minorticks_off() has no effect on a '
1235-
'logarithmic colorbar axis')
1236-
else:
1237-
long_axis.set_minor_locator(ticker.NullLocator())
1238-
12391232

12401233
@docstring.Substitution(make_axes_kw_doc)
12411234
def make_axes(parents, location=None, orientation=None, fraction=0.15,

lib/matplotlib/tests/test_colorbar.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,24 @@ def test_colorbar_minorticks_on_off():
307307
np.testing.assert_almost_equal(cbar.ax.yaxis.get_minorticklocs(),
308308
correct_minorticklocs)
309309

310+
# tests for github issue #13257 and PR #13265
311+
data = np.random.uniform(low=1, high=10, size=(20, 20))
312+
313+
fig, ax = plt.subplots()
314+
im = ax.pcolormesh(data, norm=LogNorm())
315+
cbar = fig.colorbar(im)
316+
default_minorticklocks = cbar.ax.yaxis.get_minorticklocs()
317+
318+
# test that minorticks turn off for LogNorm
319+
cbar.minorticks_off()
320+
assert np.array_equal(cbar.ax.yaxis.get_minorticklocs(),
321+
np.array([]))
322+
323+
# test that minorticks turn back on for LogNorm
324+
cbar.minorticks_on()
325+
assert np.array_equal(cbar.ax.yaxis.get_minorticklocs(),
326+
default_minorticklocks)
327+
310328

311329
def test_colorbar_autoticks():
312330
# Test new autotick modes. Needs to be classic because

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy