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, and you
289+ # can express your normalization in terms of function, you can use
290+ # `~.colors.FuncNorm`. Note that this example is the same as
291+ # `PowerNorm` with a power of 0.5:
292+
293+ def _forward (x ):
294+ return np .sqrt (x )
295+
296+
297+ def _inverse (x ):
298+ return x ** 2
299+
300+ N = 100
301+ X , Y = np .mgrid [0 :3 :complex (0 , N ), 0 :2 :complex (0 , N )]
302+ Z1 = (1 + np .sin (Y * 10. )) * X ** 2
303+ fig , ax = plt .subplots ()
304+
305+ norm = colors .FuncNorm ((_forward , _inverse ), vmin = 0 , vmax = 20 )
306+ pcm = ax .pcolormesh (X , Y , Z1 , norm = norm , cmap = 'PuBu_r' , shading = 'auto' )
307+ ax .set_title ('FuncNorm(x)' )
308+ fig .colorbar (pcm , shrink = 0.6 )
309+ plt .show ()
310+
281311###############################################################################
282312# Custom normalization: Manually implement two linear ranges
283313# ----------------------------------------------------------
284314#
285315# The `.TwoSlopeNorm` described above makes a useful example for
286316# defining your own norm.
287317
318+
288319class MidpointNormalize (colors .Normalize ):
289320 def __init__ (self , vmin = None , vmax = None , vcenter = None , clip = False ):
290321 self .vcenter = vcenter
@@ -303,5 +334,6 @@ def __call__(self, value, clip=None):
303334pcm = ax .pcolormesh (longitude , latitude , topo , rasterized = True , norm = midnorm ,
304335 cmap = terrain_map , shading = 'auto' )
305336ax .set_aspect (1 / np .cos (np .deg2rad (49 )))
337+ ax .set_title ('Custom norm' )
306338fig .colorbar (pcm , shrink = 0.6 , extend = 'both' )
307339plt .show ()
0 commit comments