Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66efef9

Browse files
authored
Merge pull request #13563 from jklymak/fix-inverted-colorbar
FIX: inverted colorbar ticks
2 parents fd40d7d + 318326e commit 66efef9

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ def __init__(self, colorbar):
238238
super().__init__(nbins=nbins, steps=steps)
239239

240240
def tick_values(self, vmin, vmax):
241+
# flip if needed:
242+
if vmin > vmax:
243+
vmin, vmax = vmax, vmin
241244
vmin = max(vmin, self._colorbar.norm.vmin)
242245
vmax = min(vmax, self._colorbar.norm.vmax)
243246
ticks = super().tick_values(vmin, vmax)
@@ -295,8 +298,10 @@ def __init__(self, colorbar, *args, **kwargs):
295298
super().__init__(*args, **kwargs)
296299

297300
def tick_values(self, vmin, vmax):
298-
vmin = self._colorbar.norm.vmin
299-
vmax = self._colorbar.norm.vmax
301+
if vmin > vmax:
302+
vmin, vmax = vmax, vmin
303+
vmin = max(vmin, self._colorbar.norm.vmin)
304+
vmax = min(vmax, self._colorbar.norm.vmax)
300305
ticks = super().tick_values(vmin, vmax)
301306
rtol = (np.log10(vmax) - np.log10(vmin)) * 1e-10
302307
ticks = ticks[(np.log10(ticks) >= np.log10(vmin) - rtol) &

lib/matplotlib/tests/test_colorbar.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,28 @@ def test_colorbar_scale_reset():
514514
def test_colorbar_get_ticks():
515515
with rc_context({'_internal.classic_mode': False}):
516516

517-
fig, ax = plt. subplots()
517+
fig, ax = plt.subplots()
518518
np.random.seed(19680801)
519519
pc = ax.pcolormesh(np.random.rand(30, 30))
520520
cb = fig.colorbar(pc)
521521
np.testing.assert_allclose(cb.get_ticks(), [0.2, 0.4, 0.6, 0.8])
522+
523+
524+
def test_colorbar_inverted_ticks():
525+
fig, axs = plt.subplots(2)
526+
ax = axs[0]
527+
pc = ax.pcolormesh(10**np.arange(1, 5).reshape(2, 2), norm=LogNorm())
528+
cbar = fig.colorbar(pc, ax=ax, extend='both')
529+
ticks = cbar.get_ticks()
530+
cbar.ax.invert_yaxis()
531+
np.testing.assert_allclose(ticks, cbar.get_ticks())
532+
533+
ax = axs[1]
534+
pc = ax.pcolormesh(np.arange(1, 5).reshape(2, 2))
535+
cbar = fig.colorbar(pc, ax=ax, extend='both')
536+
cbar.minorticks_on()
537+
ticks = cbar.get_ticks()
538+
minorticks = cbar.get_ticks(minor=True)
539+
cbar.ax.invert_yaxis()
540+
np.testing.assert_allclose(ticks, cbar.get_ticks())
541+
np.testing.assert_allclose(minorticks, cbar.get_ticks(minor=True))

0 commit comments

Comments
 (0)