1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| from copy import copy
import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors
x0, x1 = -5, 5 y0, y1 = -3, 3 x = np.linspace(x0, x1, 500) y = np.linspace(y0, y1, 500) X, Y = np.meshgrid(x, y) Z1 = np.exp(-X**2 - Y**2) Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2
palette = copy(plt.cm.gray) palette.set_over('r', 1.0) palette.set_under('g', 1.0) palette.set_bad('b', 1.0)
Zm = np.ma.masked_where(Z > 1.2, Z)
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 5.4))
im = ax1.imshow(Zm, interpolation='bilinear', cmap=palette, norm=colors.Normalize(vmin=-1.0, vmax=1.0), aspect='auto', origin='lower', extent=[x0, x1, y0, y1]) ax1.set_title('Green=low, Red=high, Blue=masked') cbar = fig.colorbar(im, extend='both', shrink=0.9, ax=ax1) cbar.set_label('uniform') for ticklabel in ax1.xaxis.get_ticklabels(): ticklabel.set_visible(False)
im = ax2.imshow(Zm, interpolation='nearest', cmap=palette, norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1], ncolors=palette.N), aspect='auto', origin='lower', extent=[x0, x1, y0, y1]) ax2.set_title('With BoundaryNorm') cbar = fig.colorbar(im, extend='both', spacing='proportional', shrink=0.9, ax=ax2) cbar.set_label('proportional')
fig.suptitle('imshow, with out-of-range and masked data') plt.show()
|