From a06f343dee3cebf035b74f65ea00b8842af448e9 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 22 Nov 2018 19:46:57 +0100 Subject: [PATCH] Simplify stride calculations in loglocator. These are just algebraic manipulations: In classic mode, we want the smallest stride so that numdec/stride+1 <= numticks i.e. stride >= numdec/(numticks-1) so stride is the ceil() of the RHS (and at least 1, to handle the case vmin==vmax). In nonclassic mode, we want the smallest stride so that numdec//stride+1 <= numticks, i.e. numdec//stride < numticks (everything is integer), i.e. numdec < numticks*stride- 1 i.e. stride > (numdec+1)/numticks so stride is 1 + the floor() of the LHS. --- lib/matplotlib/ticker.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 81a2ebfe81d4..e6bfb3225cf0 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -2182,15 +2182,10 @@ def tick_values(self, vmin, vmax): else: subs = self._subs - # get decades between major ticks. - stride = 1 - if rcParams['_internal.classic_mode']: - # Leave the bug left over from the PY2-PY3 transition. - while numdec / stride + 1 > numticks: - stride += 1 - else: - while numdec // stride + 1 > numticks: - stride += 1 + # Get decades between major ticks. + stride = (max(math.ceil(numdec / (numticks - 1)), 1) + if rcParams['_internal.classic_mode'] else + (numdec + 1) // numticks + 1) # Does subs include anything other than 1? have_subs = len(subs) > 1 or (len(subs) == 1 and subs[0] != 1.0)