From d9bf0be16df694e0aa5a95b74e30fe8d9be86403 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Fri, 12 Jun 2020 18:53:00 -1000 Subject: [PATCH 1/3] Give _DummyAxis instances a __name__ --- lib/matplotlib/tests/test_ticker.py | 26 ++++++++++++++++++++------ lib/matplotlib/ticker.py | 6 ++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 263df149de5d..611b5cf968d8 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -510,6 +510,14 @@ class TestScalarFormatter: (True, (6, 6), (-1e5, 1e5), 6, False), ] + cursor_data = [ + [0., "0.000"], + [0.0123, "0.012"], + [0.123, "0.123"], + [1.23, "1.230"], + [12.3, "12.300"], + ] + @pytest.mark.parametrize('unicode_minus, result', [(True, "\N{MINUS SIGN}1"), (False, "-1")]) def test_unicode_minus(self, unicode_minus, result): @@ -556,15 +564,21 @@ def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks): tmp_form.set_locs(ax.yaxis.get_majorticklocs()) assert orderOfMag == tmp_form.orderOfMagnitude - def test_cursor_precision(self): + @pytest.mark.parametrize('data, expected', cursor_data) + def test_cursor_precision(self, data, expected): fig, ax = plt.subplots() ax.set_xlim(-1, 1) # Pointing precision of 0.001. fmt = ax.xaxis.get_major_formatter().format_data_short - assert fmt(0.) == "0.000" - assert fmt(0.0123) == "0.012" - assert fmt(0.123) == "0.123" - assert fmt(1.23) == "1.230" - assert fmt(12.3) == "12.300" + assert fmt(data) == expected + + @pytest.mark.parametrize('data, expected', cursor_data) + def test_cursor_dummy_axis(self, data, expected): + # Issue #17624 + sf = mticker.ScalarFormatter() + sf.create_dummy_axis() + sf.set_bounds(0, 10) + fmt = sf.format_data_short + assert fmt(data) == expected class FakeAxis: diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 31dbf601e531..ac738909055d 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -198,6 +198,8 @@ class _DummyAxis: + __name__ = "dummy" + def __init__(self, minpos=0): self.dataLim = mtransforms.Bbox.unit() self.viewLim = mtransforms.Bbox.unit() @@ -708,8 +710,8 @@ def format_data_short(self, value): screen_xy + [[0, -1], [0, +1]])[:, 1] delta = abs(neighbor_values - value).max() else: - # Rough approximation: no more than 1e4 pixels. - delta = self.axis.get_view_interval() / 1e4 + # Rough approximation: no more than 1e4 divisions. + delta = np.diff(self.axis.get_view_interval()) / 1e4 # If e.g. value = 45.67 and delta = 0.02, then we want to round to # 2 digits after the decimal point (floor(log10(0.02)) = -2); # 45.67 contributes 2 digits before the decimal point From b6bdcd1086371b64e8cb4e0501ba4bb06df2d995 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Sun, 14 Jun 2020 10:57:57 -1000 Subject: [PATCH 2/3] Handle axis instances even if they don't have a __name__. --- lib/matplotlib/ticker.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index ac738909055d..5cef24be9934 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -695,7 +695,8 @@ def format_data_short(self, value): if isinstance(value, Integral): fmt = "%d" else: - if self.axis.__name__ in ["xaxis", "yaxis"]: + if (hasattr(self.axis, '__name__') and + self.axis.__name__ in ["xaxis", "yaxis"]): if self.axis.__name__ == "xaxis": axis_trf = self.axis.axes.get_xaxis_transform() axis_inv_trf = axis_trf.inverted() From f458fc493e5570210d847edb1348711e349e521c Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Mon, 15 Jun 2020 09:50:19 -1000 Subject: [PATCH 3/3] Use more compact syntax --- lib/matplotlib/ticker.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 5cef24be9934..6c288b6c9144 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -695,8 +695,7 @@ def format_data_short(self, value): if isinstance(value, Integral): fmt = "%d" else: - if (hasattr(self.axis, '__name__') and - self.axis.__name__ in ["xaxis", "yaxis"]): + if getattr(self.axis, "__name__", "") in ["xaxis", "yaxis"]: if self.axis.__name__ == "xaxis": axis_trf = self.axis.axes.get_xaxis_transform() axis_inv_trf = axis_trf.inverted()