8000 Add BoundaryNorm extend kwarg to colormapnorms. · matplotlib/matplotlib@5a98541 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a98541

Browse files
committed
Add BoundaryNorm extend kwarg to colormapnorms.
1 parent 97558ce commit 5a98541

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

tutorials/colors/colormapnorms.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@
145145
# Another normalization that comes with Matplotlib is `.colors.BoundaryNorm`.
146146
# In addition to *vmin* and *vmax*, this takes as arguments boundaries between
147147
# which data is to be mapped. The colors are then linearly distributed between
148-
# these "bounds". For instance:
148+
# these "bounds". It can also take an *extend* argument to add upper and/or
149+
# lower out-of-bounds values to the range over which the colors are
150+
# distributed For instance:
149151
#
150152
# .. ipython::
151153
#
@@ -161,30 +163,42 @@
161163
# Note: Unlike the other norms, this norm returns values from 0 to *ncolors*-1.
162164

163165
N = 100
164-
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
166+
X, Y = np.meshgrid(np.linspace(-3, 3, N), np.linspace(-2, 2, N))
165167
Z1 = np.exp(-X**2 - Y**2)
166168
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
167-
Z = (Z1 - Z2) * 2
169+
Z = ((Z1 - Z2) * 2)[:-1, :-1]
168170

169-
fig, ax = plt.subplots(3, 1, figsize=(8, 8))
171+
fig, ax = plt.subplots(2, 2, figsize=(8, 6), constrained_layout=True)
170172
ax = ax.flatten()
171-
# even bounds gives a contour-like effect
172-
bounds = np.linspace(-1, 1, 10)
173-
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
174-
pcm = ax[0].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r', shading='auto')
175-
fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical')
176173

177-
# uneven bounds changes the colormapping:
178-
bounds = np.array([-0.25, -0.125, 0, 0.5, 1])
174+
# Default norm:
175+
pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r')
176+
fig.colorbar(pcm, ax=ax[0], orientation='vertical')
177+
ax[0].set_title('Default norm')
178+
179+
# Even bounds give a contour-like effect:
180+
bounds = np.linspace(-1.5, 1.5, 7)
179181
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
180-
pcm = ax[1].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r', shading='auto')
182+
pcm = ax[1].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
181183
fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical')
184+
ax[1].set_title('BoundaryNorm: 7 boundaries')
182185

183-
pcm = ax[2].pcolormesh(X, Y, Z, cmap='RdBu_r', vmin=-np.max(Z), shading='auto')
186+
# Bounds may be unevenly spaced:
187+
bounds = np.array([-0.2, -0.1, 0, 0.5, 1])
188+
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
189+
pcm = ax[2].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
184190
fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical')
191+
ax[2].set_title('BoundaryNorm: nonuniform')
192+
193+
# With out-of-bounds colors:
194+
bounds = np.linspace(-1.5, 1.5, 7)
195+
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256, extend='both')
196+
pcm = ax[3].pcolormesh(X, Y, Z, norm=norm, cmap='RdBu_r')
197+
# The colorbar inherits the "extend" argument from BoundaryNorm.
198+
fig.colorbar(pcm, ax=ax[3], orientation='vertical')
199+
ax[3].set_title('BoundaryNorm: extend="both"')
185200
plt.show()
186201

187-
188202
###############################################################################
189203
# TwoSlopeNorm: Different mapping on either side of a center
190204
# ----------------------------------------------------------

0 commit comments

Comments
 (0)
0