From cc197649640cb00d86a425006d3854bebc320792 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 28 Jun 2021 19:18:07 +0200 Subject: [PATCH] Fix ScalarFormatter.format_ticks for non-ordered tick locations. Non-ordered "ticks" locations can be used when using ScalarFormatter to format contour lines. Perhaps ContourLabeler.get_text could arrange for the "ticks" to be ordered, but it's much easier to just support non-ordered ticks in ScalarFormatter. --- lib/matplotlib/tests/test_contour.py | 10 ++++++++++ lib/matplotlib/ticker.py | 5 +---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py index 73a7a3d6ae56..e64c5ec5a6db 100644 --- a/lib/matplotlib/tests/test_contour.py +++ b/lib/matplotlib/tests/test_contour.py @@ -464,3 +464,13 @@ def test_contour_line_start_on_corner_edge(): cbar = fig.colorbar(filled) lines = ax.contour(x, y, z, corner_mask=True, colors='k') cbar.add_lines(lines) + + +@pytest.mark.style("default") +def test_contour_autolabel_beyond_powerlimits(): + ax = plt.figure().add_subplot() + cs = plt.contour(np.geomspace(1e-6, 1e-4, 100).reshape(10, 10), + levels=[.25e-5, 1e-5, 4e-5]) + ax.clabel(cs) + # Currently, the exponent is missing, but that may be fixed in the future. + assert {text.get_text() for text in ax.texts} == {"0.25", "1.00", "4.00"} diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 2831069ab222..4e4f8d5a302b 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -817,10 +817,7 @@ def _set_order_of_magnitude(self): if self.offset: oom = math.floor(math.log10(vmax - vmin)) else: - if locs[0] > locs[-1]: - val = locs[0] - else: - val = locs[-1] + val = locs.max() if val == 0: oom = 0 else: