Skip to content

Commit a528eaa

Browse files
committed
Merge pull request matplotlib#2461 from tacaswell/new_style_format_str
Added a new style string Formatter.
2 parents 0b98a14 + b4376cd commit a528eaa

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

doc/api/api_changes.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ original location:
103103
* Added the rcParam `axes.fromatter.useoffset` to control the default value
104104
of `useOffset` in `ticker.ScalarFormatter`
105105

106-
107-
106+
* Added `Formatter` sub-class `StrMethodFormatter` which
107+
does the exact same thing as `FormatStrFormatter`, but for new-style
108+
formatting strings.
108109

109110
.. _changes_in_1_3:
110111

doc/users/whats_new.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ He also increased the performance for all of these functions and plot types.
5555

5656
Support for detrending and windowing 2D arrays in mlab
5757
``````````````````````````````````````````````````````
58-
Todd Jennings added support for 2D arrays in the
58+
Todd Jennings added support for 2D arrays in the
5959
:func:`~matplotlib.mlab.detrend_mean`, :func:`~matplotlib.mlab.detrend_none`,
60-
and :func:`~matplotlib.mlab.detrend`, as well as adding
60+
and :func:`~matplotlib.mlab.detrend`, as well as adding
6161
:func:`~matplotlib.mlab.apply_window` which support windowing 2D arrays.
6262

6363
Support for strides in mlab
@@ -68,6 +68,11 @@ strides to create memory-efficient 2D arrays. This includes
6868
array, and :func:`~matplotlib.mlab.stride_windows`, which uses a moving window
6969
to create a 2D array from a 1D array.
7070

71+
Formatter for new-style formatting strings
72+
``````````````````````````````````````````
73+
Added `FormatStrFormatterNewStyle` which does the same job as
74+
`FormatStrFormatter`, but accepts new-style formatting strings
75+
instead of printf-style formatting strings
7176

7277
Date handling
7378
-------------

lib/matplotlib/tests/test_ticker.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from numpy.testing import assert_almost_equal
88
import numpy as np
99
import matplotlib
10+
1011
import matplotlib.ticker as mticker
1112

1213

@@ -24,7 +25,7 @@ def test_MaxNLocator():
2425

2526
def test_LinearLocator():
2627
loc = mticker.LinearLocator(numticks=3)
27-
test_value = np.array([-0.8, -0.3, 0.2])
28+
test_value = np.array([-0.8, -0.3, 0.2])
2829
assert_almost_equal(loc.tick_values(-0.8, 0.2), test_value)
2930

3031

@@ -57,6 +58,16 @@ def test_use_offset():
5758
nose.tools.assert_equal(use_offset, tmp_form.get_useOffset())
5859

5960

61+
def test_formatstrformatter():
62+
# test % style formatter
63+
tmp_form = mticker.FormatStrFormatter('%05d')
64+
nose.tools.assert_equal('00002', tmp_form(2))
65+
66+
# test str.format() style formatter
67+
tmp_form = mticker.StrMethodFormatter('{x:05d}')
68+
nose.tools.assert_equal('00002', tmp_form(2))
69+
70+
6071
if __name__ == '__main__':
6172
import nose
6273
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/ticker.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def __call__(self, x, pos=None):
292292

293293
class FormatStrFormatter(Formatter):
294294
"""
295-
Use a format string to format the tick
295+
Use an old-style ('%' operator) format string to format the tick
296296
"""
297297
def __init__(self, fmt):
298298
self.fmt = fmt
@@ -302,6 +302,19 @@ def __call__(self, x, pos=None):
302302
return self.fmt % x
303303

304304

305+
class StrMethodFormatter(Formatter):
306+
"""
307+
Use a new-style format string (as used by `str.format()`)
308+
to format the tick. The field formatting must be labeled `x`.
309+
"""
310+
def __init__(self, fmt):
311+
self.fmt = fmt
312+
313+
def __call__(self, x, pos=None):
314+
'Return the format for tick val *x* at position *pos*'
315+
return self.fmt.format(x=x)
316+
317+
305318
class OldScalarFormatter(Formatter):
306319
"""
307320
Tick location is a plain old number.

0 commit comments

Comments
 (0)