Skip to content

Commit 34d90dd

Browse files
committed
Fix LogLocator locating
1 parent 9465538 commit 34d90dd

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

lib/matplotlib/tests/test_colorbar.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ def test_colorbar_autotickslog():
414414
x = np.arange(-3.0, 4.001)
415415
y = np.arange(-4.0, 3.001)
416416
X, Y = np.meshgrid(x, y)
417+
# Z ranges from -12 to 12
417418
Z = X * Y
418419
Z = Z[:-1, :-1]
419420
pcm = ax[0].pcolormesh(X, Y, 10**Z, norm=LogNorm())
@@ -423,12 +424,10 @@ def test_colorbar_autotickslog():
423424
pcm = ax[1].pcolormesh(X, Y, 10**Z, norm=LogNorm())
424425
cbar2 = fig.colorbar(pcm, ax=ax[1], extend='both',
425426
orientation='vertical', shrink=0.4)
426-
# note only -12 to +12 are visible
427427
np.testing.assert_almost_equal(cbar.ax.yaxis.get_ticklocs(),
428-
10**np.arange(-16., 16.2, 4.))
429-
# note only -24 to +24 are visible
428+
10**np.arange(-12, 12.01, 4))
430429
np.testing.assert_almost_equal(cbar2.ax.yaxis.get_ticklocs(),
431-
10**np.arange(-24., 25., 12.))
430+
10**np.arange(-12, 12.01, 12))
432431

433432

434433
def test_colorbar_get_ticks():
@@ -515,6 +514,7 @@ def test_colorbar_log_minortick_labels():
515514

516515
def test_colorbar_renorm():
517516
x, y = np.ogrid[-4:4:31j, -4:4:31j]
517+
# z ranges from ~1.52e-9 to 1.2e5
518518
z = 120000*np.exp(-x**2 - y**2)
519519

520520
fig, ax = plt.subplots()
@@ -529,7 +529,7 @@ def test_colorbar_renorm():
529529
norm = LogNorm(z.min(), z.max())
530530
im.set_norm(norm)
531531
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(),
532-
np.logspace(-10, 7, 18))
532+
np.logspace(-8, 5, 14))
533533
# note that set_norm removes the FixedLocator...
534534
assert np.isclose(cbar.vmin, z.min())
535535
cbar.set_ticks([1, 2, 3])
@@ -564,7 +564,7 @@ def test_colorbar_format():
564564
im.set_norm(LogNorm(vmin=0.1, vmax=10))
565565
fig.canvas.draw()
566566
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() ==
567-
r'$\mathdefault{10^{-2}}$')
567+
r'$\mathdefault{10^{-1}}$')
568568

569569

570570
def test_colorbar_scale_reset():

lib/matplotlib/tests/test_ticker.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,11 @@ def test_basic(self):
198198
with pytest.raises(ValueError):
199199
loc.tick_values(0, 1000)
200200

201-
test_value = np.array([1.00000000e-05, 1.00000000e-03, 1.00000000e-01,
202-
1.00000000e+01, 1.00000000e+03, 1.00000000e+05,
203-
1.00000000e+07, 1.000000000e+09])
204-
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)
201+
test_value = np.array([1e-3, 1e-1, 1e1, 1e3, 1e5])
202+
assert_almost_equal(loc.tick_values(0.9e-3, 1.1e5), test_value)
205203

206204
loc = mticker.LogLocator(base=2)
207-
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
205+
test_value = np.array([1, 2, 4, 8, 16, 32, 64])
208206
assert_almost_equal(loc.tick_values(1, 100), test_value)
209207

210208
def test_switch_to_autolocator(self):

lib/matplotlib/ticker.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,8 +2322,10 @@ def tick_values(self, vmin, vmax):
23222322

23232323
if vmax < vmin:
23242324
vmin, vmax = vmax, vmin
2325-
log_vmin = math.log(vmin) / math.log(b)
2326-
log_vmax = math.log(vmax) / math.log(b)
2325+
# Since base 10 is most common, use log10 here to minimise floating
2326+
# point errors when vmin/vmax is exactly on a decade
2327+
log_vmin = np.log10(vmin) / np.log10(b)
2328+
log_vmax = np.log10(vmax) / np.log10(b)
23272329

23282330
numdec = math.floor(log_vmax) - math.ceil(log_vmin)
23292331

@@ -2342,7 +2344,7 @@ def tick_values(self, vmin, vmax):
23422344
# Get decades between major ticks.
23432345
stride = (max(math.ceil(numdec / (numticks - 1)), 1)
23442346
if mpl.rcParams['_internal.classic_mode'] else
2345-
(numdec + 1) // numticks + 1)
2347+
numdec // numticks)
23462348

23472349
# if we have decided that the stride is as big or bigger than
23482350
# the range, clip the stride back to the available range - 1
@@ -2355,9 +2357,9 @@ def tick_values(self, vmin, vmax):
23552357
# whether we're a major or a minor locator.
23562358
have_subs = len(subs) > 1 or (len(subs) == 1 and subs[0] != 1.0)
23572359

2358-
decades = np.arange(math.floor(log_vmin) - stride,
2359-
math.ceil(log_vmax) + 2 * stride, stride)
2360-
2360+
decades = np.arange(math.ceil(log_vmin) - have_subs,
2361+
math.floor(log_vmax) + 1,
2362+
stride)
23612363
if hasattr(self, '_transform'):
23622364
ticklocs = self._transform.inverted().transform(decades)
23632365
if have_subs:
@@ -2368,7 +2370,7 @@ def tick_values(self, vmin, vmax):
23682370
ticklocs = np.array([])
23692371
else:
23702372
if have_subs:
2371-
if stride == 1:
2373+
if stride == 1 and len(decades):
23722374
ticklocs = np.concatenate(
23732375
[subs * decade_start for decade_start in b ** decades])
23742376
else:

0 commit comments

Comments
 (0)