Skip to content

Commit 6ee9a4d

Browse files
committed
Merge pull request #4717 from efiring/nonsingular
BUG: when autoscaling, handle tiny but non-zero values; closes #4318
2 parents 73ae7f9 + 604299f commit 6ee9a4d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/matplotlib/tests/test_transforms.py

+9
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,15 @@ def test_transform_angles():
552552
assert_raises(ValueError, t.transform_angles, angles, points[0:2, :])
553553

554554

555+
def test_nonsingular():
556+
# test for zero-expansion type cases; other cases may be added later
557+
zero_expansion = np.array([-0.001, 0.001])
558+
cases = [(0, np.nan), (0, 0), (0, 7.9e-317)]
559+
for args in cases:
560+
out = np.array(mtrans.nonsingular(*args))
561+
assert_array_equal(out, zero_expansion)
562+
563+
555564
if __name__ == '__main__':
556565
import nose
557566
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/transforms.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -2726,16 +2726,23 @@ def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
27262726
27272727
Returns *vmin*, *vmax*, expanded and/or swapped if necessary.
27282728
2729-
If either input is inf or NaN, or if both inputs are 0,
2730-
returns -*expander*, *expander*.
2729+
If either input is inf or NaN, or if both inputs are 0 or very
2730+
close to zero, it returns -*expander*, *expander*.
27312731
'''
27322732
if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
27332733
return -expander, expander
2734+
27342735
swapped = False
27352736
if vmax < vmin:
27362737
vmin, vmax = vmax, vmin
27372738
swapped = True
2738-
if vmax - vmin <= max(abs(vmin), abs(vmax)) * tiny:
2739+
2740+
maxabsvalue = max(abs(vmin), abs(vmax))
2741+
if maxabsvalue < (1e6 / tiny) * np.MachAr(float).xmin:
2742+
vmin = -expander
2743+
vmax = expander
2744+
2745+
elif vmax - vmin <= maxabsvalue * tiny:
27392746
if vmax == 0 and vmin == 0:
27402747
vmin = -expander
27412748
vmax = expander

0 commit comments

Comments
 (0)