Skip to content

Commit 7010b53

Browse files
committed
Add convenience function, rcParams entry, for tick label format control
svn path=/trunk/matplotlib/; revision=2935
1 parent d8ee0e4 commit 7010b53

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2006-12-17 Added rc param 'axes.formatter.limits' to control
2+
the default threshold for switching to scientific
3+
notation. Added convenience method
4+
Axes.ticklabel_format() for turning scientific notation
5+
on or off on either or both axes. - EF
6+
17
2006-12-16 Added ability to turn control scientific notation
28
in ScalarFormatter - EF
39

lib/matplotlib/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def validate_float(s):
459459
raise ValueError('Could not convert "%s" to float' % s)
460460

461461
def validate_int(s):
462-
'convert s to float or raise'
462+
'convert s to int or raise'
463463
try: return int(s)
464464
except ValueError:
465465
raise ValueError('Could not convert "%s" to int' % s)
@@ -507,6 +507,19 @@ def __call__(self, s):
507507
except ValueError:
508508
raise ValueError('Could not convert all entries to floats')
509509

510+
class validate_nseq_int:
511+
def __init__(self, n):
512+
self.n = n
513+
def __call__(self, s):
514+
'return a seq of n ints or raise'
515+
ss = s.split(',')
516+
if len(ss) != self.n:
517+
raise ValueError('You must use exactly %d comma separated values'%self.n)
518+
try: return [int(val) for val in ss]
519+
except ValueError:
520+
raise ValueError('Could not convert all entries to ints')
521+
522+
510523
def validate_color(s):
511524
'return a valid color arg'
512525
if s.lower() == 'none': return 'None'
@@ -791,6 +804,11 @@ def __call__(self, s):
791804
'axes.grid' : [False, validate_bool], # display grid or not
792805
'axes.labelsize' : [12, validate_fontsize], # fontsize of the x any y labels
793806
'axes.labelcolor' : ['k', validate_color], # color of axis label
807+
'axes.formatter.limits' : [(-7, 7), validate_nseq_int(2)],
808+
# use scientific notation if log10
809+
# of the axis range is smaller than the
810+
# first or larger than the second
811+
794812

795813
'polaraxes.grid' : [True, validate_bool], # display polar grid or not
796814

lib/matplotlib/axes.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,45 @@ def grid(self, b=None, **kwargs):
11991199
self.yaxis.grid(b, **kwargs)
12001200
grid.__doc__ = grid.__doc__%artist.kwdocd
12011201

1202+
def ticklabel_format(self, **kwargs):
1203+
"""
1204+
Convenience method for manipulating the ScalarFormatter
1205+
used by default for linear axes.
1206+
1207+
kwargs:
1208+
style = 'sci' (or 'scientific') or 'plain';
1209+
plain turns off scientific notation
1210+
axis = 'x', 'y', or 'both'
1211+
1212+
Only the major ticks are affected.
1213+
If the method is called when the ScalarFormatter is not
1214+
the one being used, an AttributeError will be raised with
1215+
no additional error message.
1216+
1217+
Additional capabilities and/or friendlier error checking may be added.
1218+
1219+
"""
1220+
style = kwargs.pop('style', '').lower()
1221+
axis = kwargs.pop('axis', 'both').lower()
1222+
if style[:3] == 'sci':
1223+
sb = True
1224+
elif style in ['plain', 'comma']:
1225+
sb = False
1226+
if style == 'plain':
1227+
cb = False
1228+
else:
1229+
cb = True
1230+
raise NotImplementedError, "comma style remains to be added"
1231+
elif style == '':
1232+
sb = None
1233+
else:
1234+
raise ValueError, "%s is not a valid style value"
1235+
if sb is not None:
1236+
if axis == 'both' or axis == 'x':
1237+
self.xaxis.major.formatter.set_scientific(sb)
1238+
if axis == 'both' or axis == 'y':
1239+
self.yaxis.major.formatter.set_scientific(sb)
1240+
12021241
def set_axis_off(self):
12031242
"""
12041243
turn off the axis
@@ -2252,7 +2291,7 @@ def plot_date(self, x, y, fmt='bo', tz=None, xdate=True, ydate=False,
22522291
a DateLocator instance) and the default tick formatter to
22532292
AutoDateFormatter (if the tick formatter is not already set to
22542293
a DateFormatter instance).
2255-
2294+
22562295
Valid kwargs are Line2D properties:
22572296
%(Line2D)s
22582297

lib/matplotlib/ticker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def __init__(self, useOffset=True, useMathText=False):
268268
self.orderOfMagnitude = 0
269269
self.format = ''
270270
self._scientific = True
271-
self._powerlimits = (-3,4)
271+
self._powerlimits = rcParams['axes.formatter.limits']
272272

273273
def __call__(self, x, pos=None):
274274
'Return the format for tick val x at position pos'
@@ -285,7 +285,7 @@ def set_powerlimits(self, lims):
285285
'''
286286
Sets size thresholds for scientific notation.
287287
288-
e.g. xaxis.set_powerlimits((-3, 4)) sets the default in
288+
e.g. xaxis.set_powerlimits((-3, 4)) sets the pre-2007 default in
289289
which scientific notation is used for numbers less than
290290
1e-3 or greater than 1e4.
291291
See also set_scientific().

matplotlibrc.template

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ numerix : %(numerix)s # numpy, Numeric or numarray
139139
#axes.labelsize : 12 # fontsize of the x any y labels
140140
#axes.labelcolor : black
141141
#axes.axisbelow : False # whether axis gridlines and ticks are below
142-
# the axes elements (lines, text, etc)
143-
142+
# the axes elements (lines, text, etc)
143+
#axes.formatter.limits : -7, 7 # use scientific notation if log10
144+
# of the axis range is smaller than the
145+
# first or larger than the second
144146

145147
polaraxes.grid : True # display grid on polar axes
146148

0 commit comments

Comments
 (0)