|
281 | 281 | cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000]) |
282 | 282 | plt.show() |
283 | 283 |
|
| 284 | +# %% |
| 285 | +# Using a linear scale on the colormap |
| 286 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 287 | +# |
| 288 | +# By default, colorbars adopt the same axis scaling as their associated norm. |
| 289 | +# For example, for a `.TwoSlopeNorm`, colormap segments are distributed |
| 290 | +# linearly and the colorbar ticks positions are spaced non-linearly (as above, |
| 291 | +# and the left-hand colorbar below). To make the tick spacing linear instead, |
| 292 | +# you can change the scale by calling ``cb.ax.set_yscale('linear')``, as shown |
| 293 | +# in the right-hand colorbar below. The ticks will then be evenly spaced, the |
| 294 | +# colormap will appear compressed in the smaller of the two slope regions. |
| 295 | + |
| 296 | +fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) |
| 297 | +divnorm = colors.TwoSlopeNorm(vmin=-500., vcenter=0, vmax=4000) |
| 298 | + |
| 299 | +for ax, title in zip([ax1, ax2], |
| 300 | + ['Default: Scaled colorbar', 'Linear colorbar spacing']): |
| 301 | + pcm = ax.pcolormesh(longitude, latitude, topo, rasterized=True, norm=divnorm, |
| 302 | + cmap=terrain_map, shading='auto') |
| 303 | + ax.set_aspect(1 / np.cos(np.deg2rad(49))) |
| 304 | + ax.set_title(title) |
| 305 | + cb = fig.colorbar(pcm, ax=ax, shrink=0.6) |
| 306 | + cb.set_ticks(np.arange(-500, 4001, 500)) |
| 307 | + |
| 308 | +# Set linear scale for the right colorbar |
| 309 | +cb.ax.set_yscale('linear') |
284 | 310 |
|
285 | 311 | # %% |
286 | 312 | # FuncNorm: Arbitrary function normalization |
|
290 | 316 | # `~.colors.FuncNorm` to define your own. Note that this example is the same |
291 | 317 | # as `~.colors.PowerNorm` with a power of 0.5: |
292 | 318 |
|
| 319 | + |
293 | 320 | def _forward(x): |
294 | 321 | return np.sqrt(x) |
295 | 322 |
|
|
0 commit comments