|
145 | 145 | # Another normalization that comes with Matplotlib is `.colors.BoundaryNorm`.
|
146 | 146 | # In addition to *vmin* and *vmax*, this takes as arguments boundaries between
|
147 | 147 | # 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: |
149 | 151 | #
|
150 | 152 | # .. ipython::
|
151 | 153 | #
|
|
161 | 163 | # Note: Unlike the other norms, this norm returns values from 0 to *ncolors*-1.
|
162 | 164 |
|
163 | 165 | 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)) |
165 | 167 | Z1 = np.exp(-X**2 - Y**2)
|
166 | 168 | Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
|
167 |
| -Z = (Z1 - Z2) * 2 |
| 169 | +Z = ((Z1 - Z2) * 2)[:-1, :-1] |
168 | 170 |
|
169 |
| -fig, ax = plt.subplots(3, 1, figsize=(8, 8)) |
| 171 | +fig, ax = plt.subplots(2, 2, figsize=(8, 6), constrained_layout=True) |
170 | 172 | 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') |
176 | 173 |
|
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) |
179 | 181 | 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') |
181 | 183 | fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical')
|
| 184 | +ax[1].set_title('BoundaryNorm: 7 boundaries') |
182 | 185 |
|
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') |
184 | 190 | 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"') |
185 | 200 | plt.show()
|
186 | 201 |
|
187 |
| - |
188 | 202 | ###############################################################################
|
189 | 203 | # TwoSlopeNorm: Different mapping on either side of a center
|
190 | 204 | # ----------------------------------------------------------
|
|
0 commit comments