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

Skip to content

Commit 279c38b

Browse files
committed
Added tests for Asinh tick locations and improved handling of zero-straddling
1 parent 8495f7b commit 279c38b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,26 @@ def test_set_params(self):
457457
lctr.set_params(None)
458458
assert lctr.numticks == 23
459459

460+
def test_linear_values(self):
461+
lctr = mticker.AsinhLocator(linear_width=100, numticks=11)
462+
463+
assert_almost_equal(lctr.tick_values(-1, 1),
464+
np.arange(-1, 1.01, 0.2))
465+
assert_almost_equal(lctr.tick_values(-0.1, 0.1),
466+
np.arange(-0.1, 0.101, 0.02))
467+
assert_almost_equal(lctr.tick_values(-0.01, 0.01),
468+
np.arange(-0.01, 0.0101, 0.002))
469+
470+
def test_wide_values(self):
471+
lctr = mticker.AsinhLocator(linear_width=0.1, numticks=11)
472+
473+
assert_almost_equal(lctr.tick_values(-100, 100),
474+
[-100, -20, -5, -1, -0.2,
475+
0, 0.2, 1, 5, 20, 100])
476+
assert_almost_equal(lctr.tick_values(-1000, 1000),
477+
[-1000, -100, -20, -3, -0.4,
478+
0, 0.4, 3, 20, 100, 1000])
479+
460480

461481
class TestScalarFormatter:
462482
offset_data = [

lib/matplotlib/ticker.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,7 @@ class AsinhLocator(Locator):
25912591
25922592
This is very unlikely to have any use beyond the AsinhScale class.
25932593
"""
2594-
def __init__(self, linear_width, numticks=12):
2594+
def __init__(self, linear_width, numticks=11):
25952595
"""
25962596
Parameters
25972597
----------
@@ -2618,12 +2618,14 @@ def __call__(self):
26182618
def tick_values(self, vmin, vmax):
26192619
# Construct a set of "on-screen" locations
26202620
# that are uniformly spaced:
2621-
ymin, ymax = self.linear_width * np.arcsinh(np.array([vmin, vmax]) / self.linear_width)
2621+
ymin, ymax = self.linear_width * np.arcsinh(np.array([vmin, vmax])
2622+
/ self.linear_width)
26222623
ys = np.linspace(ymin, ymax, self.numticks)
2623-
if (ymin * ymax) < 0:
2624+
zero_dev = np.abs(ys / (ymax - ymin))
2625+
if (ymin * ymax) < 0 and min(zero_dev) > 1e-6:
26242626
# Ensure that the zero tick-mark is included,
26252627
# if the axis stradles zero
2626-
ys = np.hstack([ys, 0.0])
2628+
ys = np.hstack([ys[(zero_dev > 0.5 / self.numticks)], 0.0])
26272629

26282630
# Transform the "on-screen" grid to the data space:
26292631
xs = self.linear_width * np.sinh(ys / self.linear_width)

0 commit comments

Comments
 (0)