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

Skip to content

Commit 103267d

Browse files
committed
Fix autoscaling with tiny sticky values.
1 parent 15c0d7c commit 103267d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,15 +2545,17 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
25452545
locator = axis.get_major_locator()
25462546
x0, x1 = locator.nonsingular(x0, x1)
25472547

2548-
# Prevent margin addition from crossing a sticky value. Small
2549-
# tolerances (whose values come from isclose()) must be used due to
2550-
# floating point issues with streamplot.
2551-
def tol(x): return 1e-5 * abs(x) + 1e-8
2548+
# Prevent margin addition from crossing a sticky value. A small
2549+
# tolerance must be added due to floating point issues with
2550+
# streamplot; it is defined relative to x0, x1, x1-x0 but has
2551+
# no absolute term (e.g. "+1e-8") to avoid issues when working with
2552+
# datasets where all values are tiny (less than 1e-8).
2553+
tol = 1e-5 * max(abs(x0), abs(x1), abs(x1 - x0))
25522554
# Index of largest element < x0 + tol, if any.
2553-
i0 = stickies.searchsorted(x0 + tol(x0)) - 1
2555+
i0 = stickies.searchsorted(x0 + tol) - 1
25542556
x0bound = stickies[i0] if i0 != -1 else None
25552557
# Index of smallest element > x1 - tol, if any.
2556-
i1 = stickies.searchsorted(x1 - tol(x1))
2558+
i1 = stickies.searchsorted(x1 - tol)
25572559
x1bound = stickies[i1] if i1 != len(stickies) else None
25582560

25592561
# Add the margin in figure space and then transform back, to handle

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6314,3 +6314,11 @@ def test_polar_interpolation_steps_variable_r(fig_test, fig_ref):
63146314
l.get_path()._interpolation_steps = 100
63156315
fig_ref.add_subplot(projection="polar").plot(
63166316
np.linspace(0, np.pi/2, 101), np.linspace(1, 2, 101))
6317+
6318+
6319+
@pytest.mark.style('default')
6320+
def test_autoscale_tiny_sticky():
6321+
fig, ax = plt.subplots()
6322+
ax.bar(0, 1e-9)
6323+
fig.canvas.draw()
6324+
assert ax.get_ylim() == (0, 1.05e-9)

0 commit comments

Comments
 (0)