4040
4141import matplotlib .pyplot as plt
4242import numpy as np
43+ import matplotlib .colors as mcolors
4344import matplotlib .gridspec as gridspec
4445
4546import matplotlib ._layoutbox as layoutbox
@@ -98,10 +99,20 @@ def example_plot(ax, fontsize=12, nodec=False):
9899# automatically. Note that if you specify ``use_gridspec=True`` it will be
99100# ignored because this option is made for improving the layout via
100101# ``tight_layout``.
102+ #
103+ # .. note::
104+ #
105+ # For the `pcolormesh` kwargs (``pc_kwargs``) we use a dictionary.
106+ # Below we will assign one colorbar to a number of axes each containing
107+ # a `ScalarMappable`; specifying the norm and colormap ensures
108+ # the colorbar is accurate for all the axes.
101109
102110arr = np .arange (100 ).reshape ((10 , 10 ))
111+ norm = mcolors .Normalize (vmin = 0. , vmax = 100. )
112+ # see note above: this makes all pcolormesh calls consistent:
113+ pc_kwargs = {'rasterized' :True , 'cmap' :'viridis' , 'norm' :norm }
103114fig , ax = plt .subplots (figsize = (4 , 4 ), constrained_layout = True )
104- im = ax .pcolormesh (arr , rasterized = True )
115+ im = ax .pcolormesh (arr , ** pc_kwargs )
105116fig .colorbar (im , ax = ax , shrink = 0.6 )
106117
107118############################################################################
@@ -110,7 +121,7 @@ def example_plot(ax, fontsize=12, nodec=False):
110121
111122fig , axs = plt .subplots (2 , 2 , figsize = (4 , 4 ), constrained_layout = True )
112123for ax in axs .flatten ():
113- im = ax .pcolormesh (arr , rasterized = True )
124+ im = ax .pcolormesh (arr , ** pc_kwargs )
114125fig .colorbar (im , ax = axs , shrink = 0.6 )
115126
116127############################################################################
@@ -122,9 +133,9 @@ def example_plot(ax, fontsize=12, nodec=False):
122133
123134fig , axs = plt .subplots (3 , 1 , figsize = (4 , 4 ), constrained_layout = True )
124135for ax in axs [:2 ]:
125- im = ax .pcolormesh (arr , rasterized = True )
136+ im = ax .pcolormesh (arr , ** pc_kwargs )
126137fig .colorbar (im , ax = axs [:2 ], shrink = 0.6 )
127- im = axs [2 ].pcolormesh (arr , rasterized = True )
138+ im = axs [2 ].pcolormesh (arr , ** pc_kwargs )
128139fig .colorbar (im , ax = axs [2 ], shrink = 0.6 )
129140
130141############################################################################
@@ -133,9 +144,9 @@ def example_plot(ax, fontsize=12, nodec=False):
133144
134145fig , axs = plt .subplots (3 , 1 , figsize = (4 , 4 ), constrained_layout = True )
135146for ax in axs [:2 ]:
136- im = ax .pcolormesh (arr , rasterized = True )
147+ im = ax .pcolormesh (arr , ** pc_kwargs )
137148fig .colorbar (im , ax = axs [:2 ], shrink = 0.6 )
138- im = axs [2 ].pcolormesh (arr , rasterized = True )
149+ im = axs [2 ].pcolormesh (arr , ** pc_kwargs )
139150fig .colorbar (im , ax = [axs [2 ]], shrink = 0.6 )
140151
141152####################################################
@@ -146,7 +157,7 @@ def example_plot(ax, fontsize=12, nodec=False):
146157
147158fig , axs = plt .subplots (2 , 2 , figsize = (4 , 4 ), constrained_layout = True )
148159for ax in axs .flatten ():
149- im = ax .pcolormesh (arr , rasterized = True )
160+ im = ax .pcolormesh (arr , ** pc_kwargs )
150161fig .colorbar (im , ax = axs , shrink = 0.6 )
151162fig .suptitle ('Big Suptitle' )
152163
@@ -225,7 +236,7 @@ def example_plot(ax, fontsize=12, nodec=False):
225236
226237fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
227238for ax in axs .flatten ():
228- pc = ax .pcolormesh (arr , rasterized = True )
239+ pc = ax .pcolormesh (arr , ** pc_kwargs )
229240 fig .colorbar (im , ax = ax , shrink = 0.6 , pad = 0 )
230241 ax .set_xticklabels ('' )
231242 ax .set_yticklabels ('' )
@@ -239,7 +250,7 @@ def example_plot(ax, fontsize=12, nodec=False):
239250
240251fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
241252for ax in axs .flatten ():
242- pc = ax .pcolormesh (arr , rasterized = True )
253+ pc = ax .pcolormesh (arr , ** pc_kwargs )
243254 fig .colorbar (im , ax = ax , shrink = 0.6 , pad = 0.05 )
244255 ax .set_xticklabels ('' )
245256 ax .set_yticklabels ('' )
@@ -349,7 +360,7 @@ def docomplicated(suptitle=None):
349360 axs = []
350361 for gs in gsr :
351362 ax = fig .add_subplot (gs )
352- pcm = ax .pcolormesh (arr , rasterized = True )
363+ pcm = ax .pcolormesh (arr , ** pc_kwargs )
353364 ax .set_xlabel ('x-label' )
354365 ax .set_ylabel ('y-label' )
355366 ax .set_title ('title' )
@@ -620,9 +631,9 @@ def docomplicated(suptitle=None):
620631# colorbar is associated wiht a gridspec.
621632
622633fig , ax = plt .subplots (1 , 2 , constrained_layout = True )
623- im = ax [0 ].pcolormesh (arr , rasterized = True )
634+ im = ax [0 ].pcolormesh (arr , ** pc_kwargs )
624635fig .colorbar (im , ax = ax [0 ], shrink = 0.6 )
625- im = ax [1 ].pcolormesh (arr , rasterized = True )
636+ im = ax [1 ].pcolormesh (arr , ** pc_kwargs )
626637plot_children (fig , fig ._layoutbox , printit = False )
627638
628639#######################################################################
@@ -637,7 +648,7 @@ def docomplicated(suptitle=None):
637648
638649fig , ax = plt .subplots (2 , 2 , constrained_layout = True )
639650for a in ax .flatten ():
640- im = a .pcolormesh (arr , rasterized = True )
651+ im = a .pcolormesh (arr , ** pc_kwargs )
641652fig .colorbar (im , ax = ax , shrink = 0.6 )
642653plot_children (fig , fig ._layoutbox , printit = False )
643654
@@ -664,11 +675,11 @@ def docomplicated(suptitle=None):
664675fig = plt .figure (constrained_layout = True )
665676gs = gridspec .GridSpec (2 , 2 , figure = fig )
666677ax = fig .add_subplot (gs [:, 0 ])
667- im = ax .pcolormesh (arr , rasterized = True )
678+ im = ax .pcolormesh (arr , ** pc_kwargs )
668679ax = fig .add_subplot (gs [0 , 1 ])
669- im = ax .pcolormesh (arr , rasterized = True )
680+ im = ax .pcolormesh (arr , ** pc_kwargs )
670681ax = fig .add_subplot (gs [1 , 1 ])
671- im = ax .pcolormesh (arr , rasterized = True )
682+ im = ax .pcolormesh (arr , ** pc_kwargs )
672683plot_children (fig , fig ._layoutbox , printit = False )
673684
674685#######################################################################
@@ -681,13 +692,13 @@ def docomplicated(suptitle=None):
681692 height_ratios = [1. , 0.5 , 1.5 ],
682693 width_ratios = [1.2 , 0.8 ])
683694ax = fig .add_subplot (gs [:2 , 0 ])
684- im = ax .pcolormesh (arr , rasterized = True )
695+ im = ax .pcolormesh (arr , ** pc_kwargs )
685696ax = fig .add_subplot (gs [2 , 0 ])
686- im = ax .pcolormesh (arr , rasterized = True )
697+ im = ax .pcolormesh (arr , ** pc_kwargs )
687698ax = fig .add_subplot (gs [0 , 1 ])
688- im = ax .pcolormesh (arr , rasterized = True )
699+ im = ax .pcolormesh (arr , ** pc_kwargs )
689700ax = fig .add_subplot (gs [1 :, 1 ])
690- im = ax .pcolormesh (arr , rasterized = True )
701+ im = ax .pcolormesh (arr , ** pc_kwargs )
691702plot_children (fig , fig ._layoutbox , printit = False )
692703
693704########################################################################
@@ -705,9 +716,9 @@ def docomplicated(suptitle=None):
705716fig = plt .figure (constrained_layout = True )
706717gs = gridspec .GridSpec (1 , 3 , figure = fig )
707718ax = fig .add_subplot (gs [0 ])
708- im = ax .pcolormesh (arr , rasterized = True )
719+ im = ax .pcolormesh (arr , ** pc_kwargs )
709720ax = fig .add_subplot (gs [- 1 ])
710- im = ax .pcolormesh (arr , rasterized = True )
721+ im = ax .pcolormesh (arr , ** pc_kwargs )
711722plot_children (fig , fig ._layoutbox , printit = False )
712723plt .show ()
713724
0 commit comments