3
3
Tick formatters
4
4
===============
5
5
6
- Show the different tick formatters.
6
+ Tick formatters define how the numeric value associated with a tick on an axis
7
+ is formatted as a string.
8
+
9
+ This example illustrates the usage and effect of the most common formatters.
7
10
"""
8
11
9
- import numpy as np
10
12
import matplotlib .pyplot as plt
11
13
import matplotlib .ticker as ticker
12
14
13
15
14
- # Setup a plot such that only the bottom spine is shown
15
- def setup (ax ):
16
+ def setup (ax , title ):
17
+ """Set up common parameters for the Axes in the example."""
18
+ # only show the bottom spine
19
+ ax .yaxis .set_major_locator (ticker .NullLocator ())
16
20
ax .spines ['right' ].set_color ('none' )
17
21
ax .spines ['left' ].set_color ('none' )
18
- ax .yaxis .set_major_locator (ticker .NullLocator ())
19
22
ax .spines ['top' ].set_color ('none' )
23
+
24
+ # define tick positions
25
+ ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
26
+ ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
27
+
20
28
ax .xaxis .set_ticks_position ('bottom' )
21
29
ax .tick_params (which = 'major' , width = 1.00 , length = 5 )
22
30
ax .tick_params (which = 'minor' , width = 0.75 , length = 2.5 , labelsize = 10 )
23
31
ax .set_xlim (0 , 5 )
24
32
ax .set_ylim (0 , 1 )
25
- ax .patch .set_alpha (0.0 )
33
+ ax .text (0.0 , 0.2 , title , transform = ax .transAxes ,
34
+ fontsize = 14 , fontname = 'Monospace' , color = 'tab:blue' )
26
35
27
36
28
- fig = plt .figure (figsize = (8 , 6 ))
29
- n = 7
37
+ fig , axs = plt .subplots (7 , 1 , figsize = (8 , 6 ))
30
38
31
39
# Null formatter
32
- ax = fig .add_subplot (n , 1 , 1 )
33
- setup (ax )
34
- ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
35
- ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
36
- ax .xaxis .set_major_formatter (ticker .NullFormatter ())
37
- ax .xaxis .set_minor_formatter (ticker .NullFormatter ())
38
- ax .text (0.0 , 0.1 , "NullFormatter()" , fontsize = 16 , transform = ax .transAxes )
40
+ setup (axs [0 ], title = "NullFormatter()" )
41
+ axs [0 ].xaxis .set_major_formatter (ticker .NullFormatter ())
39
42
40
43
# Fixed formatter
41
- ax = fig .add_subplot (n , 1 , 2 )
42
- setup (ax )
43
- ax .xaxis .set_major_locator (ticker .MultipleLocator (1.0 ))
44
- ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
45
- majors = ["" , "0" , "1" , "2" , "3" , "4" , "5" ]
46
- ax .xaxis .set_major_formatter (ticker .FixedFormatter (majors ))
47
- minors = ["" ] + ["%.2f" % (x - int (x )) if (x - int (x ))
48
- else "" for x in np .arange (0 , 5 , 0.25 )]
49
- ax .xaxis .set_minor_formatter (ticker .FixedFormatter (minors ))
50
- ax .text (0.0 , 0.1 , "FixedFormatter(['', '0', '1', ...])" ,
51
- fontsize = 15 , transform = ax .transAxes )
44
+ setup (axs [1 ], title = "FixedFormatter(['A', 'B', 'C', ...])" )
45
+ # FixedFormatter should only be used together with FixedLocator.
46
+ # Otherwise, one cannot be sure where the labels will end up.
47
+ positions = [0 , 1 , 2 , 3 , 4 , 5 ]
48
+ labels = ['A' , 'B' , 'C' , 'D' , 'E' , 'F' ]
49
+ axs [1 ].xaxis .set_major_locator (ticker .FixedLocator (positions ))
50
+ axs [1 ].xaxis .set_major_formatter (ticker .FixedFormatter (labels ))
52
51
53
52
54
53
# FuncFormatter can be used as a decorator
@@ -57,52 +56,24 @@ def major_formatter(x, pos):
57
56
return "[%.2f]" % x
58
57
59
58
60
- ax = fig .add_subplot (n , 1 , 3 )
61
- setup (ax )
62
- ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
63
- ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
64
- ax .xaxis .set_major_formatter (major_formatter )
65
- ax .text (0.0 , 0.1 , 'FuncFormatter(lambda x, pos: "[%.2f]" % x)' ,
66
- fontsize = 15 , transform = ax .transAxes )
67
-
59
+ setup (axs [2 ], title = 'FuncFormatter(lambda x, pos: "[%.2f]" % x)' )
60
+ axs [2 ].xaxis .set_major_formatter (major_formatter )
68
61
69
62
# FormatStr formatter
70
- ax = fig .add_subplot (n , 1 , 4 )
71
- setup (ax )
72
- ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
73
- ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
74
- ax .xaxis .set_major_formatter (ticker .FormatStrFormatter (">%d<" ))
75
- ax .text (0.0 , 0.1 , "FormatStrFormatter('>%d<')" ,
76
- fontsize = 15 , transform = ax .transAxes )
63
+ setup (axs [3 ], title = "FormatStrFormatter('#%d')" )
64
+ axs [3 ].xaxis .set_major_formatter (ticker .FormatStrFormatter ("#%d" ))
77
65
78
66
# Scalar formatter
79
- ax = fig .add_subplot (n , 1 , 5 )
80
- setup (ax )
81
- ax .xaxis .set_major_locator (ticker .AutoLocator ())
82
- ax .xaxis .set_minor_locator (ticker .AutoMinorLocator ())
83
- ax .xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
84
- ax .text (0.0 , 0.1 , "ScalarFormatter()" , fontsize = 15 , transform = ax .transAxes )
67
+ setup (axs [4 ], title = "ScalarFormatter()" )
68
+ axs [4 ].xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
85
69
86
70
# StrMethod formatter
87
- ax = fig .add_subplot (n , 1 , 6 )
88
- setup (ax )
89
- ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
90
- ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
91
- ax .xaxis .set_major_formatter (ticker .StrMethodFormatter ("{x}" ))
92
- ax .text (0.0 , 0.1 , "StrMethodFormatter('{x}')" ,
93
- fontsize = 15 , transform = ax .transAxes )
71
+ setup (axs [5 ], title = "StrMethodFormatter('{x:.3f}')" )
72
+ axs [5 ].xaxis .set_major_formatter (ticker .StrMethodFormatter ("{x:.3f}" ))
94
73
95
74
# Percent formatter
96
- ax = fig .add_subplot (n , 1 , 7 )
97
- setup (ax )
98
- ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
99
- ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
100
- ax .xaxis .set_major_formatter (ticker .PercentFormatter (xmax = 5 ))
101
- ax .text (0.0 , 0.1 , "PercentFormatter(xmax=5)" ,
102
- fontsize = 15 , transform = ax .transAxes )
103
-
104
- # Push the top of the top axes outside the figure because we only show the
105
- # bottom spine.
106
- fig .subplots_adjust (left = 0.05 , right = 0.95 , bottom = 0.05 , top = 1.05 )
75
+ setup (axs [6 ], title = "PercentFormatter(xmax=5)" )
76
+ axs [6 ].xaxis .set_major_formatter (ticker .PercentFormatter (xmax = 5 ))
107
77
78
+ plt .tight_layout ()
108
79
plt .show ()
0 commit comments