From eb5c4b2f285e354409987d4c0d2e8b7cb49940e0 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 12 Oct 2018 11:07:11 -0700 Subject: [PATCH 1/2] FIX: make minor ticks formatted with science formatter as well --- lib/matplotlib/colorbar.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index e147fe127a19..5c8a270852c9 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -234,12 +234,12 @@ def __init__(self, colorbar): self._colorbar = colorbar nbins = 'auto' steps = [1, 2, 2.5, 5, 10] - ticker.MaxNLocator.__init__(self, nbins=nbins, steps=steps) + super().__init__(nbins=nbins, steps=steps) def tick_values(self, vmin, vmax): vmin = max(vmin, self._colorbar.norm.vmin) vmax = min(vmax, self._colorbar.norm.vmax) - ticks = ticker.MaxNLocator.tick_values(self, vmin, vmax) + ticks = super().tick_values(vmin, vmax) return ticks[(ticks >= vmin) & (ticks <= vmax)] @@ -260,12 +260,12 @@ def __init__(self, colorbar, n=None): """ self._colorbar = colorbar self.ndivs = n - ticker.AutoMinorLocator.__init__(self, n=None) + super().__init__(n=None) def __call__(self): vmin = self._colorbar.norm.vmin vmax = self._colorbar.norm.vmax - ticks = ticker.AutoMinorLocator.__call__(self) + ticks = super().__call__() rtol = (vmax - vmin) * 1e-10 return ticks[(ticks >= vmin - rtol) & (ticks <= vmax + rtol)] @@ -290,12 +290,12 @@ def __init__(self, colorbar, *args, **kwargs): same as `~.ticker.LogLocator`. """ self._colorbar = colorbar - ticker.LogLocator.__init__(self, *args, **kwargs) + super().__init__(*args, **kwargs) def tick_values(self, vmin, vmax): vmin = self._colorbar.norm.vmin vmax = self._colorbar.norm.vmax - ticks = ticker.LogLocator.tick_values(self, vmin, vmax) + ticks = super().tick_values(vmin, vmax) return ticks[(ticks >= vmin) & (ticks <= vmax)] @@ -461,7 +461,6 @@ def config_axis(self): long_axis.set_ticks_position(self.ticklocation) short_axis.set_ticks([]) short_axis.set_ticks([], minor=True) - self._set_label() def _get_ticker_locator_formatter(self): @@ -536,7 +535,7 @@ def update_ticks(self): long_axis.set_minor_locator(_ColorbarLogLocator(self, base=10., subs='auto')) long_axis.set_minor_formatter( - ticker.LogFormatter() + ticker.LogFormatterSciNotation() ) else: _log.debug('Using fixed locator on colorbar') From e5d7fd43a7f2fb33e770f27558f05570d7842f43 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 12 Oct 2018 12:31:32 -0700 Subject: [PATCH 2/2] TST: test colorbar tick labels correct --- lib/matplotlib/tests/test_colorbar.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 56a829418910..fd2a7d52c7b6 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -396,3 +396,18 @@ def test_colorbar_axes_kw(): plt.imshow(([[1, 2], [3, 4]])) plt.colorbar(orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5, aspect=10, anchor=(0., 0.), panchor=(0., 1.)) + + +def test_colorbar_log_minortick_labels(): + with rc_context({'_internal.classic_mode': False}): + fig, ax = plt.subplots() + pcm = ax.imshow([[10000, 50000]], norm=LogNorm()) + cb = fig.colorbar(pcm) + fig.canvas.draw() + lb = cb.ax.yaxis.get_ticklabels(which='both') + expected = [r'$\mathdefault{10^{4}}$', + r'$\mathdefault{2\times10^{4}}$', + r'$\mathdefault{3\times10^{4}}$', + r'$\mathdefault{4\times10^{4}}$'] + for l, exp in zip(lb, expected): + assert l.get_text() == exp