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

Skip to content

Commit a5ecb15

Browse files
meeseeksmachinetimhoffm
authored andcommitted
Backport PR #12507: FIX: make minor ticks formatted with science formatter as well (#12517)
1 parent b8995e0 commit a5ecb15

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/matplotlib/colorbar.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ def __init__(self, colorbar):
235235
self._colorbar = colorbar
236236
nbins = 'auto'
237237
steps = [1, 2, 2.5, 5, 10]
238-
ticker.MaxNLocator.__init__(self, nbins=nbins, steps=steps)
238+
super().__init__(nbins=nbins, steps=steps)
239239

240240
def tick_values(self, vmin, vmax):
241241
vmin = max(vmin, self._colorbar.norm.vmin)
242242
vmax = min(vmax, self._colorbar.norm.vmax)
243-
ticks = ticker.MaxNLocator.tick_values(self, vmin, vmax)
243+
ticks = super().tick_values(vmin, vmax)
244244
return ticks[(ticks >= vmin) & (ticks <= vmax)]
245245

246246

@@ -261,12 +261,12 @@ def __init__(self, colorbar, n=None):
261261
"""
262262
self._colorbar = colorbar
263263
self.ndivs = n
264-
ticker.AutoMinorLocator.__init__(self, n=None)
264+
super().__init__(n=None)
265265

266266
def __call__(self):
267267
vmin = self._colorbar.norm.vmin
268268
vmax = self._colorbar.norm.vmax
269-
ticks = ticker.AutoMinorLocator.__call__(self)
269+
ticks = super().__call__()
270270
rtol = (vmax - vmin) * 1e-10
271271
return ticks[(ticks >= vmin - rtol) & (ticks <= vmax + rtol)]
272272

@@ -291,12 +291,12 @@ def __init__(self, colorbar, *args, **kwargs):
291291
same as `~.ticker.LogLocator`.
292292
"""
293293
self._colorbar = colorbar
294-
ticker.LogLocator.__init__(self, *args, **kwargs)
294+
super().__init__(*args, **kwargs)
295295

296296
def tick_values(self, vmin, vmax):
297297
vmin = self._colorbar.norm.vmin
298298
vmax = self._colorbar.norm.vmax
299-
ticks = ticker.LogLocator.tick_values(self, vmin, vmax)
299+
ticks = super().tick_values(vmin, vmax)
300300
return ticks[(ticks >= vmin) & (ticks <= vmax)]
301301

302302

@@ -462,7 +462,6 @@ def config_axis(self):
462462
long_axis.set_ticks_position(self.ticklocation)
463463
short_axis.set_ticks([])
464464
short_axis.set_ticks([], minor=True)
465-
466465
self._set_label()
467466

468467
def _get_ticker_locator_formatter(self):
@@ -537,7 +536,7 @@ def update_ticks(self):
537536
long_axis.set_minor_locator(_ColorbarLogLocator(self,
538537
base=10., subs='auto'))
539538
long_axis.set_minor_formatter(
540-
ticker.LogFormatter()
539+
ticker.LogFormatterSciNotation()
541540
)
542541
else:
543542
_log.debug('Using fixed locator on colorbar')

lib/matplotlib/tests/test_colorbar.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,18 @@ def test_colorbar_axes_kw():
396396
plt.imshow(([[1, 2], [3, 4]]))
397397
plt.colorbar(orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
398398
aspect=10, anchor=(0., 0.), panchor=(0., 1.))
399+
400+
401+
def test_colorbar_log_minortick_labels():
402+
with rc_context({'_internal.classic_mode': False}):
403+
fig, ax = plt.subplots()
404+
pcm = ax.imshow([[10000, 50000]], norm=LogNorm())
405+
cb = fig.colorbar(pcm)
406+
fig.canvas.draw()
407+
lb = cb.ax.yaxis.get_ticklabels(which='both')
408+
expected = [r'$\mathdefault{10^{4}}$',
409+
r'$\mathdefault{2\times10^{4}}$',
410+
r'$\mathdefault{3\times10^{4}}$',
411+
r'$\mathdefault{4\times10^{4}}$']
412+
for l, exp in zip(lb, expected):
413+
assert l.get_text() == exp

0 commit comments

Comments
 (0)