169169X , Y = np .mgrid [0 :3 :complex (0 , N ), 0 :2 :complex (0 , N )]
170170Z1 = (1 + np .sin (Y * 10. )) * X ** 2
171171
172- fig , ax = plt .subplots (2 , 1 )
172+ fig , ax = plt .subplots (2 , 1 , constrained_layout = True )
173173
174174pcm = ax [0 ].pcolormesh (X , Y , Z1 , norm = colors .PowerNorm (gamma = 0.5 ),
175175 cmap = 'PuBu_r' , shading = 'auto' )
176176fig .colorbar (pcm , ax = ax [0 ], extend = 'max' )
177+ ax [0 ].set_title ('PowerNorm()' )
177178
178179pcm = ax [1 ].pcolormesh (X , Y , Z1 , cmap = 'PuBu_r' , shading = 'auto' )
179180fig .colorbar (pcm , ax = ax [1 ], extend = 'max' )
181+ ax [1 ].set_title ('Normalize()' )
180182plt .show ()
181183
182184###############################################################################
274276# Simple geographic plot, set aspect ratio beecause distance between lines of
275277# longitude depends on latitude.
276278ax .set_aspect (1 / np .cos (np .deg2rad (49 )))
279+ ax .set_title ('TwoSlopeNorm(x)' )
277280fig .colorbar (pcm , shrink = 0.6 )
278281plt .show ()
279282
280283
284+ ###############################################################################
285+ # FuncNorm: Arbitrary function normalization
286+ # ------------------------------------------
287+ #
288+ # If the above norms do not provide the normalization you want, you can use
289+ # `~.colors.FuncNorm` to define your own. Note that this example is the same
290+ # as `~.colors.PowerNorm` with a power of 0.5:
291+
292+ def _forward (x ):
293+ return np .sqrt (x )
294+
295+
296+ def _inverse (x ):
297+ return x ** 2
298+
299+ N = 100
300+ X , Y = np .mgrid [0 :3 :complex (0 , N ), 0 :2 :complex (0 , N )]
301+ Z1 = (1 + np .sin (Y * 10. )) * X ** 2
302+ fig , ax = plt .subplots ()
303+
304+ norm = colors .FuncNorm ((_forward , _inverse ), vmin = 0 , vmax = 20 )
305+ pcm = ax .pcolormesh (X , Y , Z1 , norm = norm , cmap = 'PuBu_r' , shading = 'auto' )
306+ ax .set_title ('FuncNorm(x)' )
307+ fig .colorbar (pcm , shrink = 0.6 )
308+ plt .show ()
309+
281310###############################################################################
282311# Custom normalization: Manually implement two linear ranges
283312# ----------------------------------------------------------
284313#
285314# The `.TwoSlopeNorm` described above makes a useful example for
286315# defining your own norm.
287316
317+
288318class MidpointNormalize (colors .Normalize ):
289319 def __init__ (self , vmin = None , vmax = None , vcenter = None , clip = False ):
290320 self .vcenter = vcenter
@@ -303,5 +333,6 @@ def __call__(self, value, clip=None):
303333pcm = ax .pcolormesh (longitude , latitude , topo , rasterized = True , norm = midnorm ,
304334 cmap = terrain_map , shading = 'auto' )
305335ax .set_aspect (1 / np .cos (np .deg2rad (49 )))
336+ ax .set_title ('Custom norm' )
306337fig .colorbar (pcm , shrink = 0.6 , extend = 'both' )
307338plt .show ()
0 commit comments