@@ -34,59 +34,76 @@ def setup(ax, title):
3434 fontsize = 14 , fontname = 'Monospace' , color = 'tab:blue' )
3535
3636
37- fig , axs = plt .subplots (9 , 1 , figsize = (8 , 8 ))
37+ # Tick formatters can be set in one of two ways, either by passing a ``str``
38+ # or function to `~.Axis.set_major_formatter` or `~.Axis.set_minor_formatter`,
39+ # or by creating an instance of one of the various `~.ticker.Formatter` classes
40+ # and providing that to `~.Axis.set_major_formatter` or
41+ # `~.Axis.set_minor_formatter`.
3842
39- # A string, using format string function syntax, can be used directly as a
43+ # The first two examples directly pass a ``str`` or function.
44+
45+ fig0 , axs0 = plt .subplots (2 , 1 , figsize = (8 , 2 ))
46+ fig0 .suptitle ('Simple Formatting' )
47+
48+ # A ``str``, using format string function syntax, can be used directly as a
4049# formatter. The variable ``x`` is the tick value and the variable ``pos`` is
4150# tick position. This creates a StrMethodFormatter automatically.
42- setup (axs [0 ], title = "'{x} km'" )
43- axs [0 ].xaxis .set_major_formatter ('{x} km' )
51+ setup (axs0 [0 ], title = "'{x} km'" )
52+ axs0 [0 ].xaxis .set_major_formatter ('{x} km' )
4453
4554# A function can also be used directly as a formatter. The function must take
4655# two arguments: ``x`` for the tick value and ``pos`` for the tick position,
4756# and must return a ``str`` This creates a FuncFormatter automatically.
48- setup (axs [1 ], title = "lambda x, pos: str(x-5)" )
49- axs [1 ].xaxis .set_major_formatter (lambda x , pos : str (x - 5 ))
57+ setup (axs0 [1 ], title = "lambda x, pos: str(x-5)" )
58+ axs0 [1 ].xaxis .set_major_formatter (lambda x , pos : str (x - 5 ))
59+
60+ fig0 .tight_layout ()
61+
62+
63+ # The remaining examples use Formatter objects.
64+
65+ fig1 , axs1 = plt .subplots (7 , 1 , figsize = (8 , 6 ))
66+ fig1 .suptitle ('Formatter Object Formatting' )
5067
5168# Null formatter
52- setup (axs [ 2 ], title = "NullFormatter()" )
53- axs [ 2 ].xaxis .set_major_formatter (ticker .NullFormatter ())
69+ setup (axs1 [ 0 ], title = "NullFormatter()" )
70+ axs1 [ 0 ].xaxis .set_major_formatter (ticker .NullFormatter ())
5471
5572# StrMethod formatter
56- setup (axs [ 3 ], title = "StrMethodFormatter('{x:.3f}')" )
57- axs [ 3 ].xaxis .set_major_formatter (ticker .StrMethodFormatter ("{x:.3f}" ))
73+ setup (axs1 [ 1 ], title = "StrMethodFormatter('{x:.3f}')" )
74+ axs1 [ 1 ].xaxis .set_major_formatter (ticker .StrMethodFormatter ("{x:.3f}" ))
5875
5976# FuncFormatter can be used as a decorator
6077@ticker .FuncFormatter
6178def major_formatter (x , pos ):
6279 return f'[{ x :.2f} ]'
6380
6481
65- setup (axs [ 4 ], title = 'FuncFormatter("[{:.2f}]".format' )
66- axs [ 4 ].xaxis .set_major_formatter (major_formatter )
82+ setup (axs1 [ 2 ], title = 'FuncFormatter("[{:.2f}]".format' )
83+ axs1 [ 2 ].xaxis .set_major_formatter (major_formatter )
6784
6885# Fixed formatter
69- setup (axs [ 5 ], title = "FixedFormatter(['A', 'B', 'C', ...])" )
86+ setup (axs1 [ 3 ], title = "FixedFormatter(['A', 'B', 'C', ...])" )
7087# FixedFormatter should only be used together with FixedLocator.
7188# Otherwise, one cannot be sure where the labels will end up.
7289positions = [0 , 1 , 2 , 3 , 4 , 5 ]
7390labels = ['A' , 'B' , 'C' , 'D' , 'E' , 'F' ]
74- axs [ 5 ].xaxis .set_major_locator (ticker .FixedLocator (positions ))
75- axs [ 5 ].xaxis .set_major_formatter (ticker .FixedFormatter (labels ))
91+ axs1 [ 3 ].xaxis .set_major_locator (ticker .FixedLocator (positions ))
92+ axs1 [ 3 ].xaxis .set_major_formatter (ticker .FixedFormatter (labels ))
7693
7794# Scalar formatter
78- setup (axs [ 6 ], title = "ScalarFormatter()" )
79- axs [ 6 ].xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
95+ setup (axs1 [ 4 ], title = "ScalarFormatter()" )
96+ axs1 [ 4 ].xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
8097
8198# FormatStr formatter
82- setup (axs [ 7 ], title = "FormatStrFormatter('#%d')" )
83- axs [ 7 ].xaxis .set_major_formatter (ticker .FormatStrFormatter ("#%d" ))
99+ setup (axs1 [ 5 ], title = "FormatStrFormatter('#%d')" )
100+ axs1 [ 5 ].xaxis .set_major_formatter (ticker .FormatStrFormatter ("#%d" ))
84101
85102# Percent formatter
86- setup (axs [ 8 ], title = "PercentFormatter(xmax=5)" )
87- axs [ 8 ].xaxis .set_major_formatter (ticker .PercentFormatter (xmax = 5 ))
103+ setup (axs1 [ 6 ], title = "PercentFormatter(xmax=5)" )
104+ axs1 [ 6 ].xaxis .set_major_formatter (ticker .PercentFormatter (xmax = 5 ))
88105
89- fig .tight_layout ()
106+ fig1 .tight_layout ()
90107plt .show ()
91108
92109
0 commit comments