8
8
9
9
This example illustrates the usage and effect of the most common formatters.
10
10
11
+ The tick format is configured via the function `~.Axis.set_major_formatter`
12
+ or `~.Axis.set_minor_formatter`. It accepts:
13
+
14
+ - a format string (this implicitly creates a `.StrMethodFormatter`)
15
+ - a function (this implicitly creates a `.FuncFormatter`)
16
+ - an instance of a `.Formatter` subclass. The most common are
17
+
18
+ - `.NullFormatter`: No labels on the ticks.
19
+ - `.StrMethodFormatter`: Use string `str.format` method.
20
+ - `.FormatStrFormatter`: Use %-style formatting.
21
+ - `.FuncFormatter`: Define labels through a function.
22
+ - `.FixedFormatter`: Set the label strings explicitly.
23
+ - `.ScalarFormatter`: Default formatter for scalars: autopick the format string.
24
+ - `.PercentFormatter`: Format labels as a percentage.
25
+
26
+ See :ref:`formatters` for a complete list.
27
+
11
28
"""
12
29
13
30
import matplotlib .pyplot as plt
@@ -34,79 +51,58 @@ def setup(ax, title):
34
51
fontsize = 14 , fontname = 'Monospace' , color = 'tab:blue' )
35
52
36
53
37
- # %%
38
- # Tick formatters can be set in one of two ways, either by passing a ``str``
39
- # or function to `~.Axis.set_major_formatter` or `~.Axis.set_minor_formatter`,
40
- # or by creating an instance of one of the various `~.ticker.Formatter` classes
41
- # and providing that to `~.Axis.set_major_formatter` or
42
- # `~.Axis.set_minor_formatter`.
43
- #
44
- # The first two examples directly pass a ``str`` or function.
45
54
46
- fig0 , axs0 = plt .subplots ( 2 , 1 , figsize = (8 , 2 ) )
47
- fig0 . suptitle ( 'Simple Formatting' )
55
+ fig = plt .figure ( figsize = (8 , 8 ), layout = 'constrained' )
56
+ fig0 , fig1 , fig2 = fig . subfigures ( 3 , height_ratios = [ 1.5 , 1.5 , 7.5 ] )
48
57
49
- # A ``str``, using format string function syntax, can be used directly as a
50
- # formatter. The variable ``x`` is the tick value and the variable ``pos`` is
51
- # tick position. This creates a StrMethodFormatter automatically.
52
- setup (axs0 [0 ], title = "'{x} km'" )
53
- axs0 [0 ].xaxis .set_major_formatter ('{x} km' )
58
+ fig0 .suptitle ('String Formatting' , fontsize = 16 , x = 0 , ha = 'left' )
59
+ ax0 = fig0 .subplots ()
54
60
55
- # A function can also be used directly as a formatter. The function must take
56
- # two arguments: ``x`` for the tick value and ``pos`` for the tick position,
57
- # and must return a ``str``. This creates a FuncFormatter automatically.
58
- setup (axs0 [1 ], title = "lambda x, pos: str(x-5)" )
59
- axs0 [1 ].xaxis .set_major_formatter (lambda x , pos : str (x - 5 ))
61
+ setup (ax0 , title = "'{x} km'" )
62
+ ax0 .xaxis .set_major_formatter ('{x} km' )
60
63
61
- fig0 .tight_layout ()
62
64
65
+ fig1 .suptitle ('Function Formatting' , fontsize = 16 , x = 0 , ha = 'left' )
66
+ ax1 = fig1 .subplots ()
67
+
68
+ setup (ax1 , title = "def(x, pos): return str(x-5)" )
69
+ ax1 .xaxis .set_major_formatter (lambda x , pos : str (x - 5 ))
63
70
64
- # %%
65
- # The remaining examples use `.Formatter` objects.
66
71
67
- fig1 , axs1 = plt . subplots ( 7 , 1 , figsize = ( 8 , 6 ) )
68
- fig1 . suptitle ( 'Formatter Object Formatting' )
72
+ fig2 . suptitle ( 'Formatter Object Formatting' , fontsize = 16 , x = 0 , ha = 'left' )
73
+ axs2 = fig2 . subplots ( 7 , 1 )
69
74
70
- # Null formatter
71
- setup (axs1 [0 ], title = "NullFormatter()" )
72
- axs1 [0 ].xaxis .set_major_formatter (ticker .NullFormatter ())
75
+ setup (axs2 [0 ], title = "NullFormatter()" )
76
+ axs2 [0 ].xaxis .set_major_formatter (ticker .NullFormatter ())
73
77
74
- # StrMethod formatter
75
- setup (axs1 [1 ], title = "StrMethodFormatter('{x:.3f}')" )
76
- axs1 [1 ].xaxis .set_major_formatter (ticker .StrMethodFormatter ("{x:.3f}" ))
78
+ setup (axs2 [1 ], title = "StrMethodFormatter('{x:.3f}')" )
79
+ axs2 [1 ].xaxis .set_major_formatter (ticker .StrMethodFormatter ("{x:.3f}" ))
77
80
81
+ setup (axs2 [2 ], title = "FormatStrFormatter('#%d')" )
82
+ axs2 [2 ].xaxis .set_major_formatter (ticker .FormatStrFormatter ("#%d" ))
78
83
79
- # FuncFormatter can be used as a decorator
80
- @ticker .FuncFormatter
81
- def major_formatter (x , pos ):
84
+
85
+ def fmt_two_digits (x , pos ):
82
86
return f'[{ x :.2f} ]'
83
87
84
88
85
- setup (axs1 [ 2 ], title = 'FuncFormatter("[{:.2f}]".format)' )
86
- axs1 [ 2 ].xaxis .set_major_formatter (major_formatter )
89
+ setup (axs2 [ 3 ], title = 'FuncFormatter("[{:.2f}]".format)' )
90
+ axs2 [ 3 ].xaxis .set_major_formatter (ticker . FuncFormatter ( fmt_two_digits ) )
87
91
88
- # Fixed formatter
89
- setup (axs1 [3 ], title = "FixedFormatter(['A', 'B', 'C', ...])" )
92
+ setup (axs2 [4 ], title = "FixedFormatter(['A', 'B', 'C', 'D', 'E', 'F'])" )
90
93
# FixedFormatter should only be used together with FixedLocator.
91
94
# Otherwise, one cannot be sure where the labels will end up.
92
95
positions = [0 , 1 , 2 , 3 , 4 , 5 ]
93
96
labels = ['A' , 'B' , 'C' , 'D' , 'E' , 'F' ]
94
- axs1 [3 ].xaxis .set_major_locator (ticker .FixedLocator (positions ))
95
- axs1 [3 ].xaxis .set_major_formatter (ticker .FixedFormatter (labels ))
96
-
97
- # Scalar formatter
98
- setup (axs1 [4 ], title = "ScalarFormatter()" )
99
- axs1 [4 ].xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
97
+ axs2 [4 ].xaxis .set_major_locator (ticker .FixedLocator (positions ))
98
+ axs2 [4 ].xaxis .set_major_formatter (ticker .FixedFormatter (labels ))
100
99
101
- # FormatStr formatter
102
- setup (axs1 [5 ], title = "FormatStrFormatter('#%d')" )
103
- axs1 [5 ].xaxis .set_major_formatter (ticker .FormatStrFormatter ("#%d" ))
100
+ setup (axs2 [5 ], title = "ScalarFormatter()" )
101
+ axs2 [5 ].xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
104
102
105
- # Percent formatter
106
- setup (axs1 [6 ], title = "PercentFormatter(xmax=5)" )
107
- axs1 [6 ].xaxis .set_major_formatter (ticker .PercentFormatter (xmax = 5 ))
103
+ setup (axs2 [6 ], title = "PercentFormatter(xmax=5)" )
104
+ axs2 [6 ].xaxis .set_major_formatter (ticker .PercentFormatter (xmax = 5 ))
108
105
109
- fig1 .tight_layout ()
110
106
plt .show ()
111
107
112
108
0 commit comments