Skip to content

Commit 79fba06

Browse files
authored
Merge pull request #16469 from pharshalp/cbar_minorticks
FIX: colorbar minorticks when rcParams['x/ytick.minor.visible'] = True
2 parents 36a3a3f + cef2d95 commit 79fba06

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/matplotlib/colorbar.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,12 @@ def _config_axis(self):
517517

518518
if self.orientation == 'vertical':
519519
long_axis, short_axis = ax.yaxis, ax.xaxis
520+
if mpl.rcParams['ytick.minor.visible']:
521+
self.minorticks_on()
520522
else:
521523
long_axis, short_axis = ax.xaxis, ax.yaxis
524+
if mpl.rcParams['xtick.minor.visible']:
525+
self.minorticks_on()
522526

523527
long_axis.set_label_position(self.ticklocation)
524528
long_axis.set_ticks_position(self.ticklocation)

lib/matplotlib/tests/test_colorbar.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ def test_colorbar_minorticks_on_off():
327327
np.testing.assert_equal(cbar.ax.yaxis.get_minorticklocs(), [])
328328

329329

330+
def test_cbar_minorticks_for_rc_xyminortickvisible():
331+
"""
332+
issue gh-16468.
333+
334+
Making sure that minor ticks on the colorbar are turned on
335+
(internally) using the cbar.minorticks_on() method when
336+
rcParams['xtick.minor.visible'] = True (for horizontal cbar)
337+
rcParams['ytick.minor.visible'] = True (for vertical cbar).
338+
Using cbar.minorticks_on() ensures that the minor ticks
339+
don't overflow into the extend regions of the colorbar.
340+
"""
341+
342+
plt.rcParams['ytick.minor.visible'] = True
343+
plt.rcParams['xtick.minor.visible'] = True
344+
345+
vmin, vmax = 0.4, 2.6
346+
fig, ax = plt.subplots()
347+
im = ax.pcolormesh([[1, 2]], vmin=vmin, vmax=vmax)
348+
349+
cbar = fig.colorbar(im, extend='both', orientation='vertical')
350+
assert cbar.ax.yaxis.get_minorticklocs()[0] >= vmin
351+
assert cbar.ax.yaxis.get_minorticklocs()[-1] <= vmax
352+
353+
cbar = fig.colorbar(im, extend='both', orientation='horizontal')
354+
assert cbar.ax.xaxis.get_minorticklocs()[0] >= vmin
355+
assert cbar.ax.xaxis.get_minorticklocs()[-1] <= vmax
356+
357+
330358
def test_colorbar_autoticks():
331359
# Test new autotick modes. Needs to be classic because
332360
# non-classic doesn't go this route.

0 commit comments

Comments
 (0)