8000 Backport PR #12656: FIX: fix error in colorbar.get_ticks not having v… · matplotlib/matplotlib@744154d · GitHub
[go: up one dir, main page]

Skip to content

Commit 744154d

Browse files
jklymakMeeseeksDev[bot]
authored andcommitted
Backport PR #12656: FIX: fix error in colorbar.get_ticks not having valid data
1 parent 4644c9f commit 744154d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/matplotlib/colorbar.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ def __init__(self, ax, cmap=None,
387387
self.outline = None
388388
self.patch = None
389389
self.dividers = None
390+
self._manual_tick_data_values = None
390391

391392
if ticklocation == 'auto':
392393
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
@@ -574,7 +575,17 @@ def set_ticks(self, ticks, update_ticks=True):
574575

575576
def get_ticks(self, minor=False):
576577
"""Return the x ticks as a list of locations"""
577-
return self._tick_data_values
578+
if self._manual_tick_data_values is None:
579+
ax = self.ax
580+
if self.orientation == 'vertical':
581+
long_axis, short_axis = ax.yaxis, ax.xaxis
582+
else:
583+
long_axis, short_axis = ax.xaxis, ax.yaxis
584+
return long_axis.get_majorticklocs()
585+
else:
586+
# We made the axes manually, the old way, and the ylim is 0-1,
587+
# so the majorticklocs are in those units, not data units.
588+
return self._manual_tick_data_values
578589

579590
def set_ticklabels(self, ticklabels, update_ticks=True):
580591
"""
@@ -756,7 +767,7 @@ def _ticker(self, locator, formatter):
756767
else:
757768
eps = (intv[1] - intv[0]) * 1e-10
758769
b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)]
759-
self._tick_data_values = b
770+
self._manual_tick_data_values = b
760771
ticks = self._locate(b)
761772
formatter.set_locs(b)
762773
ticklabels = [formatter(t, i) for i, t in enumerate(b)]

lib/matplotlib/tests/test_colorbar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,13 @@ def test_colorbar_renorm():
436436
cbar.update_normal(im)
437437
assert np.isclose(cbar.vmin, z.min() * 1000)
438438
assert np.isclose(cbar.vmax, z.max() * 1000)
439+
440+
441+
def test_colorbar_get_ticks():
442+
with rc_context({'_internal.classic_mode': False}):
443+
444+
fig, ax = plt. subplots()
445+
np.random.seed(19680801)
446+
pc = ax.pcolormesh(np.random.rand(30, 30))
447+
cb = fig.colorbar(pc)
448+
np.testing.assert_allclose(cb.get_ticks(), [0.2, 0.4, 0.6, 0.8])

0 commit comments

Comments
 (0)
0