Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 04c734c

Browse filesBrowse files
committed
Added new-style formatter function.
Added class `StrMethodFormatter` which is identical to `FormatStrFormatter` only for new-style ('{}'.format) formatting methods.
1 parent f123e30 commit 04c734c
Copy full SHA for 04c734c

File tree

Expand file treeCollapse file tree

4 files changed

+33
-6
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+33
-6
lines changed

‎doc/api/api_changes.rst

Copy file name to clipboardExpand all lines: doc/api/api_changes.rst
+3-2Lines changed: 3 additions & 2 deletions
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

Copy file name to clipboardExpand all lines: doc/users/whats_new.rst
+7-2Lines changed: 7 additions & 2 deletions
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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_ticker.py
+10-1Lines changed: 10 additions & 1 deletion
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+
from itertools import izip
1011
import matplotlib.ticker as mticker
1112

1213

@@ -49,13 +50,21 @@ def test_LogLocator():
4950
test_value = np.array([0.5, 1., 2., 4., 8., 16., 32., 64., 128., 256.])
5051
assert_almost_equal(loc.tick_values(1, 100), test_value)
5152

52-
5353
def test_use_offset():
5454
for use_offset in [True, False]:
5555
with matplotlib.rc_context({'axes.formatter.useoffset': use_offset}):
5656
tmp_form = mticker.ScalarFormatter()
5757
nose.tools.assert_equal(use_offset, tmp_form.get_useOffset())
5858

59+
def test_formatstrformatter():
60+
# test % style formatter
61+
tmp_form = mticker.FormatStrFormatter('%05d')
62+
nose.tools.assert_equal('00002', tmp_form(2))
63+
64+
# test str.format() style formatter
65+
tmp_form = mticker.StrMethodFormatter('{:05d}')
66+
nose.tools.assert_equal('00002', tmp_form(2))
67+
5968

6069
if __name__ == '__main__':
6170
import nose

‎lib/matplotlib/ticker.py

Copy file name to clipboardExpand all lines: lib/matplotlib/ticker.py
+13-1Lines changed: 13 additions & 1 deletion
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,18 @@ 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()`) to format the tick
308+
"""
309+
def __init__(self, fmt):
310+
self.fmt = fmt
311+
312+
def __call__(self, x, pos=None):
313+
'Return the format for tick val *x* at position *pos*'
314+
return self.fmt.format(x)
315+
316+
305317
class OldScalarFormatter(Formatter):
306318
"""
307319
Tick location is a plain old number.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.