Skip to content

Commit c20aee9

Browse files
committed
Improve access to ScalarFormatter useOffset and offset parameters
svn path=/trunk/matplotlib/; revision=8241
1 parent 6c0c91d commit c20aee9

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2010-04-18 Control ScalarFormatter offsets directly and via the
2+
Axes.ticklabel_format() method, and add that to pyplot. -EF
3+
14
2010-04-16 Add a close_event to the backends. -RM
25

36
2010-04-06 modify axes_grid examples to use axes_grid1 and axisartist. -JJL

boilerplate.py

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def %(func)s(%(argspec)s):
101101
'table',
102102
'text',
103103
'annotate',
104+
'ticklabel_format',
104105
)
105106

106107
cmappable = {

lib/matplotlib/axes.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,11 @@ def ticklabel_format(self, **kwargs):
18801880
be used for numbers outside the range
18811881
10`-m`:sup: to 10`n`:sup:.
18821882
Use (0,0) to include all numbers.
1883+
*useOffset* [True | False | offset]; if True,
1884+
the offset will be calculated as needed;
1885+
if False, no offset will be used; if a
1886+
numeric offset is specified, it will be
1887+
used.
18831888
*axis* [ 'x' | 'y' | 'both' ]
18841889
============ =====================================
18851890
@@ -1892,13 +1897,14 @@ def ticklabel_format(self, **kwargs):
18921897
"""
18931898
style = kwargs.pop('style', '').lower()
18941899
scilimits = kwargs.pop('scilimits', None)
1900+
useOffset = kwargs.pop('useOffset', None)
1901+
axis = kwargs.pop('axis', 'both').lower()
18951902
if scilimits is not None:
18961903
try:
18971904
m, n = scilimits
18981905
m+n+1 # check that both are numbers
18991906
except (ValueError, TypeError):
19001907
raise ValueError("scilimits must be a sequence of 2 integers")
1901-
axis = kwargs.pop('axis', 'both').lower()
19021908
if style[:3] == 'sci':
19031909
sb = True
19041910
elif style in ['plain', 'comma']:
@@ -1923,6 +1929,11 @@ def ticklabel_format(self, **kwargs):
19231929
self.xaxis.major.formatter.set_powerlimits(scilimits)
19241930
if axis == 'both' or axis == 'y':
19251931
self.yaxis.major.formatter.set_powerlimits(scilimits)
1932+
if useOffset is not None:
1933+
if axis == 'both' or axis == 'x':
1934+
self.xaxis.major.formatter.set_useOffset(useOffset)
1935+
if axis == 'both' or axis == 'y':
1936+
self.yaxis.major.formatter.set_useOffset(useOffset)
19261937
except AttributeError:
19271938
raise AttributeError(
19281939
"This method only works with the ScalarFormatter.")

lib/matplotlib/pyplot.py

+8
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,14 @@ def annotate(*args, **kwargs):
25272527
draw_if_interactive()
25282528
return ret
25292529

2530+
# This function was autogenerated by boilerplate.py. Do not edit as
2531+
# changes will be lost
2532+
@docstring.copy_dedent(Axes.ticklabel_format)
2533+
def ticklabel_format(**kwargs):
2534+
ret = gca().ticklabel_format(**kwargs)
2535+
draw_if_interactive()
2536+
return ret
2537+
25302538
# This function was autogenerated by boilerplate.py. Do not edit as
25312539
# changes will be lost
25322540
def autumn():

lib/matplotlib/ticker.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,33 @@ class ScalarFormatter(Formatter):
321321
data < 10^-n or data >= 10^m, where n and m are the power limits set using
322322
set_powerlimits((n,m)). The defaults for these are controlled by the
323323
axes.formatter.limits rc parameter.
324+
324325
"""
325326

326327
def __init__(self, useOffset=True, useMathText=False):
327328
# useOffset allows plotting small data ranges with large offsets:
328329
# for example: [1+1e-9,1+2e-9,1+3e-9]
329330
# useMathText will render the offset and scientific notation in mathtext
330-
self._useOffset = useOffset
331+
self.set_useOffset(useOffset)
331332
self._usetex = rcParams['text.usetex']
332333
self._useMathText = useMathText
333-
self.offset = 0
334334
self.orderOfMagnitude = 0
335335
self.format = ''
336336
self._scientific = True
337337
self._powerlimits = rcParams['axes.formatter.limits']
338338

339+
def get_useOffset(self):
340+
return self._useOffset
341+
342+
def set_useOffset(self, val):
343+
if val in [True, False]:
344+
self.offset = 0
345+
self._useOffset = val
346+
else:
347+
self._useOffset = False
348+
self.offset = val
349+
350+
useOffset = property(fget=get_useOffset, fset=set_useOffset)
339351

340352
def fix_minus(self, s):
341353
'use a unicode minus rather than hyphen'
@@ -412,7 +424,8 @@ def set_locs(self, locs):
412424
if len(self.locs) > 0:
413425
vmin, vmax = self.axis.get_view_interval()
414426
d = abs(vmax-vmin)
415-
if self._useOffset: self._set_offset(d)
427+
if self._useOffset:
428+
self._set_offset(d)
416429
self._set_orderOfMagnitude(d)
417430
self._set_format()
418431

0 commit comments

Comments
 (0)