Skip to content

Commit d026762

Browse files
committed
ScalarFormatter: improve format string to address issue 617
1 parent c92d19f commit d026762

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lib/matplotlib/ticker.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,19 @@ def _set_orderOfMagnitude(self,range):
498498

499499
def _set_format(self):
500500
# set the format string to format all the ticklabels
501-
# The floating point black magic (adding 1e-15 and formatting
502-
# to 8 digits) may warrant review and cleanup.
503-
locs = (np.asarray(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15
504-
sigfigs = [len(str('%1.8f'% loc).split('.')[1].rstrip('0')) \
505-
for loc in locs]
506-
sigfigs.sort()
507-
self.format = '%1.' + str(sigfigs[-1]) + 'f'
501+
locs = (np.asarray(self.locs)-self.offset) / 10**self.orderOfMagnitude
502+
loc_range_oom = int(math.floor(math.log10(np.ptp(locs))))
503+
# first estimate:
504+
sigfigs = max(0, 3 - loc_range_oom)
505+
# refined estimate:
506+
thresh = 1e-3 * 10**loc_range_oom
507+
while sigfigs >= 0:
508+
if np.abs(locs - np.round(locs, decimals=sigfigs)).max() < thresh:
509+
sigfigs -= 1
510+
else:
511+
break
512+
sigfigs += 1
513+
self.format = '%1.' + str(sigfigs) + 'f'
508514
if self._usetex:
509515
self.format = '$%s$' % self.format
510516
elif self._useMathText:

0 commit comments

Comments
 (0)