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

Skip to content

Commit c8e7865

Browse files
authored
Merge pull request #17625 from efiring/ScalarFormatter_axis
Give _DummyAxis instances a __name__
2 parents 1686a5a + f458fc4 commit c8e7865

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,14 @@ class TestScalarFormatter:
510510
(True, (6, 6), (-1e5, 1e5), 6, False),
511511
]
512512

513+
cursor_data = [
514+
[0., "0.000"],
515+
[0.0123, "0.012"],
516+
[0.123, "0.123"],
517+
[1.23, "1.230"],
518+
[12.3, "12.300"],
519+
]
520+
513521
@pytest.mark.parametrize('unicode_minus, result',
514522
[(True, "\N{MINUS SIGN}1"), (False, "-1")])
515523
def test_unicode_minus(self, unicode_minus, result):
@@ -556,15 +564,21 @@ def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks):
556564
tmp_form.set_locs(ax.yaxis.get_majorticklocs())
557565
assert orderOfMag == tmp_form.orderOfMagnitude
558566

559-
def test_cursor_precision(self):
567+
@pytest.mark.parametrize('data, expected', cursor_data)
568+
def test_cursor_precision(self, data, expected):
560569
fig, ax = plt.subplots()
561570
ax.set_xlim(-1, 1) # Pointing precision of 0.001.
562571
fmt = ax.xaxis.get_major_formatter().format_data_short
563-
assert fmt(0.) == "0.000"
564-
assert fmt(0.0123) == "0.012"
565-
assert fmt(0.123) == "0.123"
566-
assert fmt(1.23) == "1.230"
567-
assert fmt(12.3) == "12.300"
572+
assert fmt(data) == expected
573+
574+
@pytest.mark.parametrize('data, expected', cursor_data)
575+
def test_cursor_dummy_axis(self, data, expected):
576+
# Issue #17624
577+
sf = mticker.ScalarFormatter()
578+
sf.create_dummy_axis()
579+
sf.set_bounds(0, 10)
580+
fmt = sf.format_data_short
581+
assert fmt(data) == expected
568582

569583

570584
class FakeAxis:

lib/matplotlib/ticker.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@
198198

199199

200200
class _DummyAxis:
201+
__name__ = "dummy"
202+
201203
def __init__(self, minpos=0):
202204
self.dataLim = mtransforms.Bbox.unit()
203205
self.viewLim = mtransforms.Bbox.unit()
@@ -693,7 +695,7 @@ def format_data_short(self, value):
693695
if isinstance(value, Integral):
694696
fmt = "%d"
695697
else:
696-
if self.axis.__name__ in ["xaxis", "yaxis"]:
698+
if getattr(self.axis, "__name__", "") in ["xaxis", "yaxis"]:
697699
if self.axis.__name__ == "xaxis":
698700
axis_trf = self.axis.axes.get_xaxis_transform()
699701
axis_inv_trf = axis_trf.inverted()
@@ -708,8 +710,8 @@ def format_data_short(self, value):
708710
screen_xy + [[0, -1], [0, +1]])[:, 1]
709711
delta = abs(neighbor_values - value).max()
710712
else:
711-
# Rough approximation: no more than 1e4 pixels.
712-
delta = self.axis.get_view_interval() / 1e4
713+
# Rough approximation: no more than 1e4 divisions.
714+
delta = np.diff(self.axis.get_view_interval()) / 1e4
713715
# If e.g. value = 45.67 and delta = 0.02, then we want to round to
714716
# 2 digits after the decimal point (floor(log10(0.02)) = -2);
715717
# 45.67 contributes 2 digits before the decimal point

0 commit comments

Comments
 (0)