33Tick formatters
44===============
55
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.
710"""
811
9- import numpy as np
1012import matplotlib .pyplot as plt
1113import matplotlib .ticker as ticker
1214
1315
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 ())
1620 ax .spines ['right' ].set_color ('none' )
1721 ax .spines ['left' ].set_color ('none' )
18- ax .yaxis .set_major_locator (ticker .NullLocator ())
1922 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+
2028 ax .xaxis .set_ticks_position ('bottom' )
2129 ax .tick_params (which = 'major' , width = 1.00 , length = 5 )
2230 ax .tick_params (which = 'minor' , width = 0.75 , length = 2.5 , labelsize = 10 )
2331 ax .set_xlim (0 , 5 )
2432 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' )
2635
2736
28- fig = plt .figure (figsize = (8 , 6 ))
29- n = 7
37+ fig , axs = plt .subplots (7 , 1 , figsize = (8 , 6 ))
3038
3139# 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 ())
3942
4043# 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 ))
5251
5352
5453# FuncFormatter can be used as a decorator
@@ -57,52 +56,24 @@ def major_formatter(x, pos):
5756 return "[%.2f]" % x
5857
5958
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 )
6861
6962# 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" ))
7765
7866# 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 ))
8569
8670# 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}" ))
9473
9574# 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 ))
10777
78+ plt .tight_layout ()
10879plt .show ()
0 commit comments