40
40
41
41
import matplotlib .pyplot as plt
42
42
import numpy as np
43
+ import matplotlib .colors as mcolors
43
44
import matplotlib .gridspec as gridspec
44
45
45
46
import matplotlib ._layoutbox as layoutbox
@@ -98,10 +99,20 @@ def example_plot(ax, fontsize=12, nodec=False):
98
99
# automatically. Note that if you specify ``use_gridspec=True`` it will be
99
100
# ignored because this option is made for improving the layout via
100
101
# ``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.
101
109
102
110
arr = 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 }
103
114
fig , ax = plt .subplots (figsize = (4 , 4 ), constrained_layout = True )
104
- im = ax .pcolormesh (arr , rasterized = True )
115
+ im = ax .pcolormesh (arr , ** pc_kwargs )
105
116
fig .colorbar (im , ax = ax , shrink = 0.6 )
106
117
107
118
############################################################################
@@ -110,7 +121,7 @@ def example_plot(ax, fontsize=12, nodec=False):
110
121
111
122
fig , axs = plt .subplots (2 , 2 , figsize = (4 , 4 ), constrained_layout = True )
112
123
for ax in axs .flatten ():
113
- im = ax .pcolormesh (arr , rasterized = True )
124
+ im = ax .pcolormesh (arr , ** pc_kwargs )
114
125
fig .colorbar (im , ax = axs , shrink = 0.6 )
115
126
116
127
############################################################################
@@ -122,9 +133,9 @@ def example_plot(ax, fontsize=12, nodec=False):
122
133
123
134
fig , axs = plt .subplots (3 , 1 , figsize = (4 , 4 ), constrained_layout = True )
124
135
for ax in axs [:2 ]:
125
- im = ax .pcolormesh (arr , rasterized = True )
136
+ im = ax .pcolormesh (arr , ** pc_kwargs )
126
137
fig .colorbar (im , ax = axs [:2 ], shrink = 0.6 )
127
- im = axs [2 ].pcolormesh (arr , rasterized = True )
138
+ im = axs [2 ].pcolormesh (arr , ** pc_kwargs )
128
139
fig .colorbar (im , ax = axs [2 ], shrink = 0.6 )
129
140
130
141
############################################################################
@@ -133,9 +144,9 @@ def example_plot(ax, fontsize=12, nodec=False):
133
144
134
145
fig , axs = plt .subplots (3 , 1 , figsize = (4 , 4 ), constrained_layout = True )
135
146
for ax in axs [:2 ]:
136
- im = ax .pcolormesh (arr , rasterized = True )
147
+ im = ax .pcolormesh (arr , ** pc_kwargs )
137
148
fig .colorbar (im , ax = axs [:2 ], shrink = 0.6 )
138
- im = axs [2 ].pcolormesh (arr , rasterized = True )
149
+ im = axs [2 ].pcolormesh (arr , ** pc_kwargs )
139
150
fig .colorbar (im , ax = [axs [2 ]], shrink = 0.6 )
140
151
141
152
####################################################
@@ -146,7 +157,7 @@ def example_plot(ax, fontsize=12, nodec=False):
146
157
147
158
fig , axs = plt .subplots (2 , 2 , figsize = (4 , 4 ), constrained_layout = True )
148
159
for ax in axs .flatten ():
149
- im = ax .pcolormesh (arr , rasterized = True )
160
+ im = ax .pcolormesh (arr , ** pc_kwargs )
150
161
fig .colorbar (im , ax = axs , shrink = 0.6 )
151
162
fig .suptitle ('Big Suptitle' )
152
163
@@ -225,7 +236,7 @@ def example_plot(ax, fontsize=12, nodec=False):
225
236
226
237
fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
227
238
for ax in axs .flatten ():
228
- pc = ax .pcolormesh (arr , rasterized = True )
239
+ pc = ax .pcolormesh (arr , ** pc_kwargs )
229
240
fig .colorbar (im , ax = ax , shrink = 0.6 , pad = 0 )
230
241
ax .set_xticklabels ('' )
231
242
ax .set_yticklabels ('' )
@@ -239,7 +250,7 @@ def example_plot(ax, fontsize=12, nodec=False):
239
250
240
251
fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
241
252
for ax in axs .flatten ():
242
- pc = ax .pcolormesh (arr , rasterized = True )
253
+ pc = ax .pcolormesh (arr , ** pc_kwargs )
243
254
fig .colorbar (im , ax = ax , shrink = 0.6 , pad = 0.05 )
244
255
ax .set_xticklabels ('' )
245
256
ax .set_yticklabels ('' )
@@ -349,7 +360,7 @@ def docomplicated(suptitle=None):
349
360
axs = []
350
361
for gs in gsr :
351
362
ax = fig .add_subplot (gs )
352
- pcm = ax .pcolormesh (arr , rasterized = True )
363
+ pcm = ax .pcolormesh (arr , ** pc_kwargs )
353
364
ax .set_xlabel ('x-label' )
354
365
ax .set_ylabel ('y-label' )
355
366
ax .set_title ('title' )
@@ -620,9 +631,9 @@ def docomplicated(suptitle=None):
620
631
# colorbar is associated wiht a gridspec.
621
632
622
633
fig , ax = plt .subplots (1 , 2 , constrained_layout = True )
623
- im = ax [0 ].pcolormesh (arr , rasterized = True )
634
+ im = ax [0 ].pcolormesh (arr , ** pc_kwargs )
624
635
fig .colorbar (im , ax = ax [0 ], shrink = 0.6 )
625
- im = ax [1 ].pcolormesh (arr , rasterized = True )
636
+ im = ax [1 ].pcolormesh (arr , ** pc_kwargs )
626
637
plot_children (fig , fig ._layoutbox , printit = False )
627
638
628
639
#######################################################################
@@ -637,7 +648,7 @@ def docomplicated(suptitle=None):
637
648
638
649
fig , ax = plt .subplots (2 , 2 , constrained_layout = True )
639
650
for a in ax .flatten ():
640
- im = a .pcolormesh (arr , rasterized = True )
651
+ im = a .pcolormesh (arr , ** pc_kwargs )
641
652
fig .colorbar (im , ax = ax , shrink = 0.6 )
642
653
plot_children (fig , fig ._layoutbox , printit = False )
643
654
@@ -664,11 +675,11 @@ def docomplicated(suptitle=None):
664
675
fig = plt .figure (constrained_layout = True )
665
676
gs = gridspec .GridSpec (2 , 2 , figure = fig )
666
677
ax = fig .add_subplot (gs [:, 0 ])
667
- im = ax .pcolormesh (arr , rasterized = True )
678
+ im = ax .pcolormesh (arr , ** pc_kwargs )
668
679
ax = fig .add_subplot (gs [0 , 1 ])
669
- im = ax .pcolormesh (arr , rasterized = True )
680
+ im = ax .pcolormesh (arr , ** pc_kwargs )
670
681
ax = fig .add_subplot (gs [1 , 1 ])
671
- im = ax .pcolormesh (arr , rasterized = True )
682
+ im = ax .pcolormesh (arr , ** pc_kwargs )
672
683
plot_children (fig , fig ._layoutbox , printit = False )
673
684
674
685
#######################################################################
@@ -681,13 +692,13 @@ def docomplicated(suptitle=None):
681
692
height_ratios = [1. , 0.5 , 1.5 ],
682
693
width_ratios = [1.2 , 0.8 ])
683
694
ax = fig .add_subplot (gs [:2 , 0 ])
684
- im = ax .pcolormesh (arr , rasterized = True )
695
+ im = ax .pcolormesh (arr , ** pc_kwargs )
685
696
ax = fig .add_subplot (gs [2 , 0 ])
686
- im = ax .pcolormesh (arr , rasterized = True )
697
+ im = ax .pcolormesh (arr , ** pc_kwargs )
687
698
ax = fig .add_subplot (gs [0 , 1 ])
688
- im = ax .pcolormesh (arr , rasterized = True )
699
+ im = ax .pcolormesh (arr , ** pc_kwargs )
689
700
ax = fig .add_subplot (gs [1 :, 1 ])
690
- im = ax .pcolormesh (arr , rasterized = True )
701
+ im = ax .pcolormesh (arr , ** pc_kwargs )
691
702
plot_children (fig , fig ._layoutbox , printit = False )
692
703
693
704
########################################################################
@@ -705,9 +716,9 @@ def docomplicated(suptitle=None):
705
716
fig = plt .figure (constrained_layout = True )
706
717
gs = gridspec .GridSpec (1 , 3 , figure = fig )
707
718
ax = fig .add_subplot (gs [0 ])
708
- im = ax .pcolormesh (arr , rasterized = True )
719
+ im = ax .pcolormesh (arr , ** pc_kwargs )
709
720
ax = fig .add_subplot (gs [- 1 ])
710
- im = ax .pcolormesh (arr , rasterized = True )
721
+ im = ax .pcolormesh (arr , ** pc_kwargs )
711
722
plot_children (fig , fig ._layoutbox , printit = False )
712
723
plt .show ()
713
724
0 commit comments