Skip to content

Commit 222e478

Browse files
authored
Merge pull request #10850 from matplotlib/auto-backport-of-pr-10849
Backport PR #10849 on branch v2.2.2-doc
2 parents 6dc0c02 + 1d79e30 commit 222e478

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import matplotlib.pyplot as plt
4242
import numpy as np
43+
import matplotlib.colors as mcolors
4344
import matplotlib.gridspec as gridspec
4445

4546
import 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

102110
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}
103114
fig, ax = plt.subplots(figsize=(4, 4), constrained_layout=True)
104-
im = ax.pcolormesh(arr, rasterized=True)
115+
im = ax.pcolormesh(arr, **pc_kwargs)
105116
fig.colorbar(im, ax=ax, shrink=0.6)
106117

107118
############################################################################
@@ -110,7 +121,7 @@ def example_plot(ax, fontsize=12, nodec=False):
110121

111122
fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
112123
for ax in axs.flatten():
113-
im = ax.pcolormesh(arr, rasterized=True)
124+
im = ax.pcolormesh(arr, **pc_kwargs)
114125
fig.colorbar(im, ax=axs, shrink=0.6)
115126

116127
############################################################################
@@ -122,9 +133,9 @@ def example_plot(ax, fontsize=12, nodec=False):
122133

123134
fig, axs = plt.subplots(3, 1, figsize=(4, 4), constrained_layout=True)
124135
for ax in axs[:2]:
125-
im = ax.pcolormesh(arr, rasterized=True)
136+
im = ax.pcolormesh(arr, **pc_kwargs)
126137
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)
128139
fig.colorbar(im, ax=axs[2], shrink=0.6)
129140

130141
############################################################################
@@ -133,9 +144,9 @@ def example_plot(ax, fontsize=12, nodec=False):
133144

134145
fig, axs = plt.subplots(3, 1, figsize=(4, 4), constrained_layout=True)
135146
for ax in axs[:2]:
136-
im = ax.pcolormesh(arr, rasterized=True)
147+
im = ax.pcolormesh(arr, **pc_kwargs)
137148
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)
139150
fig.colorbar(im, ax=[axs[2]], shrink=0.6)
140151

141152
####################################################
@@ -146,7 +157,7 @@ def example_plot(ax, fontsize=12, nodec=False):
146157

147158
fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
148159
for ax in axs.flatten():
149-
im = ax.pcolormesh(arr, rasterized=True)
160+
im = ax.pcolormesh(arr, **pc_kwargs)
150161
fig.colorbar(im, ax=axs, shrink=0.6)
151162
fig.suptitle('Big Suptitle')
152163

@@ -225,7 +236,7 @@ def example_plot(ax, fontsize=12, nodec=False):
225236

226237
fig, axs = plt.subplots(2, 2, constrained_layout=True)
227238
for 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

240251
fig, axs = plt.subplots(2, 2, constrained_layout=True)
241252
for 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

622633
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)
624635
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)
626637
plot_children(fig, fig._layoutbox, printit=False)
627638

628639
#######################################################################
@@ -637,7 +648,7 @@ def docomplicated(suptitle=None):
637648

638649
fig, ax = plt.subplots(2, 2, constrained_layout=True)
639650
for a in ax.flatten():
640-
im = a.pcolormesh(arr, rasterized=True)
651+
im = a.pcolormesh(arr, **pc_kwargs)
641652
fig.colorbar(im, ax=ax, shrink=0.6)
642653
plot_children(fig, fig._layoutbox, printit=False)
643654

@@ -664,11 +675,11 @@ def docomplicated(suptitle=None):
664675
fig = plt.figure(constrained_layout=True)
665676
gs = gridspec.GridSpec(2, 2, figure=fig)
666677
ax = fig.add_subplot(gs[:, 0])
667-
im = ax.pcolormesh(arr, rasterized=True)
678+
im = ax.pcolormesh(arr, **pc_kwargs)
668679
ax = fig.add_subplot(gs[0, 1])
669-
im = ax.pcolormesh(arr, rasterized=True)
680+
im = ax.pcolormesh(arr, **pc_kwargs)
670681
ax = fig.add_subplot(gs[1, 1])
671-
im = ax.pcolormesh(arr, rasterized=True)
682+
im = ax.pcolormesh(arr, **pc_kwargs)
672683
plot_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])
683694
ax = fig.add_subplot(gs[:2, 0])
684-
im = ax.pcolormesh(arr, rasterized=True)
695+
im = ax.pcolormesh(arr, **pc_kwargs)
685696
ax = fig.add_subplot(gs[2, 0])
686-
im = ax.pcolormesh(arr, rasterized=True)
697+
im = ax.pcolormesh(arr, **pc_kwargs)
687698
ax = fig.add_subplot(gs[0, 1])
688-
im = ax.pcolormesh(arr, rasterized=True)
699+
im = ax.pcolormesh(arr, **pc_kwargs)
689700
ax = fig.add_subplot(gs[1:, 1])
690-
im = ax.pcolormesh(arr, rasterized=True)
701+
im = ax.pcolormesh(arr, **pc_kwargs)
691702
plot_children(fig, fig._layoutbox, printit=False)
692703

693704
########################################################################
@@ -705,9 +716,9 @@ def docomplicated(suptitle=None):
705716
fig = plt.figure(constrained_layout=True)
706717
gs = gridspec.GridSpec(1, 3, figure=fig)
707718
ax = fig.add_subplot(gs[0])
708-
im = ax.pcolormesh(arr, rasterized=True)
719+
im = ax.pcolormesh(arr, **pc_kwargs)
709720
ax = fig.add_subplot(gs[-1])
710-
im = ax.pcolormesh(arr, rasterized=True)
721+
im = ax.pcolormesh(arr, **pc_kwargs)
711722
plot_children(fig, fig._layoutbox, printit=False)
712723
plt.show()
713724

0 commit comments

Comments
 (0)