@@ -25,11 +25,11 @@ def setup(ax):
2525 ax .patch .set_alpha (0.0 )
2626
2727
28- plt .figure (figsize = (8 , 6 ))
28+ fig = plt .figure (figsize = (8 , 6 ))
2929n = 7
3030
3131# Null formatter
32- ax = plt . subplot (n , 1 , 1 )
32+ ax = fig . add_subplot (n , 1 , 1 )
3333setup (ax )
3434ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
3535ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
@@ -38,7 +38,7 @@ def setup(ax):
3838ax .text (0.0 , 0.1 , "NullFormatter()" , fontsize = 16 , transform = ax .transAxes )
3939
4040# Fixed formatter
41- ax = plt . subplot (n , 1 , 2 )
41+ ax = fig . add_subplot (n , 1 , 2 )
4242setup (ax )
4343ax .xaxis .set_major_locator (ticker .MultipleLocator (1.0 ))
4444ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
@@ -51,22 +51,23 @@ def setup(ax):
5151 fontsize = 15 , transform = ax .transAxes )
5252
5353
54- # Func formatter
54+ # FuncFormatter can be used as a decorator
55+ @ticker .FuncFormatter
5556def major_formatter (x , pos ):
5657 return "[%.2f]" % x
5758
5859
59- ax = plt . subplot (n , 1 , 3 )
60+ ax = fig . add_subplot (n , 1 , 3 )
6061setup (ax )
6162ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
6263ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
63- ax .xaxis .set_major_formatter (ticker . FuncFormatter ( major_formatter ) )
64+ ax .xaxis .set_major_formatter (major_formatter )
6465ax .text (0.0 , 0.1 , 'FuncFormatter(lambda x, pos: "[%.2f]" % x)' ,
6566 fontsize = 15 , transform = ax .transAxes )
6667
6768
6869# FormatStr formatter
69- ax = plt . subplot (n , 1 , 4 )
70+ ax = fig . add_subplot (n , 1 , 4 )
7071setup (ax )
7172ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
7273ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
@@ -75,15 +76,15 @@ def major_formatter(x, pos):
7576 fontsize = 15 , transform = ax .transAxes )
7677
7778# Scalar formatter
78- ax = plt . subplot (n , 1 , 5 )
79+ ax = fig . add_subplot (n , 1 , 5 )
7980setup (ax )
8081ax .xaxis .set_major_locator (ticker .AutoLocator ())
8182ax .xaxis .set_minor_locator (ticker .AutoMinorLocator ())
8283ax .xaxis .set_major_formatter (ticker .ScalarFormatter (useMathText = True ))
8384ax .text (0.0 , 0.1 , "ScalarFormatter()" , fontsize = 15 , transform = ax .transAxes )
8485
8586# StrMethod formatter
86- ax = plt . subplot (n , 1 , 6 )
87+ ax = fig . add_subplot (n , 1 , 6 )
8788setup (ax )
8889ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
8990ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
@@ -92,7 +93,7 @@ def major_formatter(x, pos):
9293 fontsize = 15 , transform = ax .transAxes )
9394
9495# Percent formatter
95- ax = plt . subplot (n , 1 , 7 )
96+ ax = fig . add_subplot (n , 1 , 7 )
9697setup (ax )
9798ax .xaxis .set_major_locator (ticker .MultipleLocator (1.00 ))
9899ax .xaxis .set_minor_locator (ticker .MultipleLocator (0.25 ))
@@ -102,6 +103,6 @@ def major_formatter(x, pos):
102103
103104# Push the top of the top axes outside the figure because we only show the
104105# bottom spine.
105- plt .subplots_adjust (left = 0.05 , right = 0.95 , bottom = 0.05 , top = 1.05 )
106+ fig .subplots_adjust (left = 0.05 , right = 0.95 , bottom = 0.05 , top = 1.05 )
106107
107108plt .show ()
0 commit comments