| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1300,9 +1300,11 @@ def __call__(self, value, clip=None): | |||
| 1300 | 1300 | ||
| 1301 | 1301 | if not self.vmin <= self.vcenter <= self.vmax: | |
| 1302 | 1302 | raise ValueError("vmin, vcenter, vmax must increase monotonically") | |
| 1303 | + # note that we must extrapolate for tick locators: | ||
| 1303 | 1304 | result = np.ma.masked_array( | |
| 1304 | 1305 | np.interp(result, [self.vmin, self.vcenter, self.vmax], | |
| 1305 | - [0, 0.5, 1.]), mask=np.ma.getmask(result)) | ||
| 1306 | + [0, 0.5, 1], left=-np.inf, right=np.inf), | ||
| 1307 | + mask=np.ma.getmask(result)) | ||
| 1306 | 1308 | if is_scalar: | |
| 1307 | 1309 | result = np.atleast_1d(result)[0] | |
| 1308 | 1310 | return result | |
@@ -1313,8 +1315,8 @@ def inverse(self, value): | |||
| 1313 | 1315 | (vmin,), _ = self.process_value(self.vmin) | |
| 1314 | 1316 | (vmax,), _ = self.process_value(self.vmax) | |
| 1315 | 1317 | (vcenter,), _ = self.process_value(self.vcenter) | |
| 1316 | - | ||
| 1317 | - result = np.interp(value, [0, 0.5, 1.], [vmin, vcenter, vmax]) | ||
| 1318 | + result = np.interp(value, [0, 0.5, 1], [vmin, vcenter, vmax], | ||
| 1319 | + left=-np.inf, right=np.inf) | ||
| 1318 | 1320 | return result | |
| 1319 | 1321 | ||
| 1320 | 1322 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -765,11 +765,12 @@ def test_inset_colorbar_layout(): | |||
| 765 | 765 | @image_comparison(['colorbar_twoslope.png'], remove_text=True, | |
| 766 | 766 | style='mpl20') | |
| 767 | 767 | def test_twoslope_colorbar(): | |
| 768 | - # Note that the first tick = 20, and should be in the middle | ||
| 768 | + # Note that the second tick = 20, and should be in the middle | ||
| 769 | 769 | # of the colorbar (white) | |
| 770 | + # There should be no tick right at the bottom, nor at the top. | ||
| 770 | 771 | fig, ax = plt.subplots() | |
| 771 | 772 | ||
| 772 | - norm = mcolors.TwoSlopeNorm(20, 0, 100) | ||
| 773 | + norm = mcolors.TwoSlopeNorm(20, 5, 95) | ||
| 773 | 774 | pc = ax.pcolormesh(np.arange(1, 11), np.arange(1, 11), | |
| 774 | 775 | np.arange(100).reshape(10, 10), | |
| 775 | 776 | norm=norm, cmap='RdBu_r') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -277,7 +277,8 @@ | |||
| 277 | 277 | # longitude depends on latitude. | |
| 278 | 278 | ax.set_aspect(1 / np.cos(np.deg2rad(49))) | |
| 279 | 279 | ax.set_title('TwoSlopeNorm(x)') | |
| 280 | - fig.colorbar(pcm, shrink=0.6) | ||
| 280 | + cb = fig.colorbar(pcm, shrink=0.6) | ||
| 281 | + cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000]) | ||
| 281 | 282 | plt.show() | |
| 282 | 283 | ||
| 283 | 284 | ||
@@ -312,7 +313,8 @@ def _inverse(x): | |||
| 312 | 313 | # ---------------------------------------------------------- | |
| 313 | 314 | # | |
| 314 | 315 | # The `.TwoSlopeNorm` described above makes a useful example for | |
| 315 | - # defining your own norm. | ||
| 316 | + # defining your own norm. Note for the colorbar to work, you must | ||
| 317 | + # define an inverse for your norm: | ||
| 316 | 318 | ||
| 317 | 319 | ||
| 318 | 320 | class MidpointNormalize(colors.Normalize): | |
@@ -323,8 +325,14 @@ def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False): | |||
| 323 | 325 | def __call__(self, value, clip=None): | |
| 324 | 326 | # I'm ignoring masked values and all kinds of edge cases to make a | |
| 325 | 327 | # simple example... | |
| 326 | - x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1] | ||
| 327 | - return np.ma.masked_array(np.interp(value, x, y)) | ||
| 328 | + # Note also that we must extrapolate beyond vmin/vmax | ||
| 329 | + x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1.] | ||
| 330 | + return np.ma.masked_array(np.interp(value, x, y, | ||
| 331 | + left=-np.inf, right=np.inf)) | ||
| 332 | + | ||
| 333 | + def inverse(self, value): | ||
| 334 | + y, x = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1] | ||
| 335 | + return np.interp(value, x, y, left=-np.inf, right=np.inf) | ||
| 328 | 336 | ||
| 329 | 337 | ||
| 330 | 338 | fig, ax = plt.subplots() | |
@@ -334,5 +342,7 @@ def __call__(self, value, clip=None): | |||
| 334 | 342 | cmap=terrain_map, shading='auto') | |
| 335 | 343 | ax.set_aspect(1 / np.cos(np.deg2rad(49))) | |
| 336 | 344 | ax.set_title('Custom norm') | |
| 337 | - fig.colorbar(pcm, shrink=0.6, extend='both') | ||
| 345 | + cb = fig.colorbar(pcm, shrink=0.6, extend='both') | ||
| 346 | + cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000]) | ||
| 347 | + | ||
| 338 | 348 | plt.show() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments