|
5 | 5 | get a filled contour effect.
|
6 | 6 | """
|
7 | 7 | from copy import copy
|
8 |
| -from numpy import ma |
9 |
| -import matplotlib.colors as colors |
| 8 | + |
| 9 | +import numpy as np |
10 | 10 | import matplotlib.pyplot as plt
|
| 11 | +import matplotlib.colors as colors |
11 | 12 | import matplotlib.mlab as mlab
|
12 |
| -import numpy as np |
13 | 13 |
|
14 | 14 | # compute some interesting data
|
15 |
| -delta = 0.025 |
16 |
| -x = y = np.arange(-3.0, 3.0, delta) |
| 15 | +x0, x1 = -5, 5 |
| 16 | +y0, y1 = -3, 3 |
| 17 | +x = np.linspace(x0, x1, 500) |
| 18 | +y = np.linspace(y0, y1, 500) |
17 | 19 | X, Y = np.meshgrid(x, y)
|
18 | 20 | Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
|
19 | 21 | Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
|
|
31 | 33 | # If you comment out all the palette.set* lines, you will see
|
32 | 34 | # all the defaults; under and over will be colored with the
|
33 | 35 | # first and last colors in the palette, respectively.
|
34 |
| -Zm = ma.masked_where(Z > 1.2, Z) |
| 36 | +Zm = np.ma.masked_where(Z > 1.2, Z) |
35 | 37 |
|
36 | 38 | # By setting vmin and vmax in the norm, we establish the
|
37 | 39 | # range to which the regular palette color scale is applied.
|
38 | 40 | # Anything above that range is colored based on palette.set_over, etc.
|
39 | 41 |
|
40 | 42 | # set up the axes
|
41 |
| -fig, (ax1, ax2) = plt.subplots(1, 2) |
| 43 | +fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 5.4)) |
42 | 44 |
|
43 | 45 | # plot using 'continuous' color map
|
44 | 46 | im = ax1.imshow(Zm, interpolation='bilinear',
|
45 | 47 | cmap=palette,
|
46 |
| - norm=colors.Normalize(vmin=-1.0, vmax=1.0, clip=False), |
47 |
| - origin='lower', extent=[-3, 3, -3, 3]) |
48 |
| -ax1.set_title('Green=low, Red=high, Blue=bad') |
49 |
| -fig.colorbar(im, extend='both', orientation='horizontal', shrink=0.8, ax=ax1) |
| 48 | + norm=colors.Normalize(vmin=-1.0, vmax=1.0), |
| 49 | + aspect='auto', |
| 50 | + origin='lower', |
| 51 | + extent=[x0, x1, y0, y1]) |
| 52 | +ax1.set_title('Green=low, Red=high, Blue=masked') |
| 53 | +cbar = fig.colorbar(im, extend='both', shrink=0.9, ax=ax1) |
| 54 | +cbar.set_label('uniform') |
| 55 | +for ticklabel in ax1.xaxis.get_ticklabels(): |
| 56 | + ticklabel.set_visible(False) |
50 | 57 |
|
51 |
| -# plot using 'discrete' color map |
| 58 | +# Plot using a small number of colors, with unevenly spaced boundaries. |
52 | 59 | im = ax2.imshow(Zm, interpolation='nearest',
|
53 | 60 | cmap=palette,
|
54 | 61 | norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1],
|
55 |
| - ncolors=256, clip=False), |
56 |
| - origin='lower', extent=[-3, 3, -3, 3]) |
| 62 | + ncolors=palette.N), |
| 63 | + aspect='auto', |
| 64 | + origin='lower', |
| 65 | + extent=[x0, x1, y0, y1]) |
57 | 66 | ax2.set_title('With BoundaryNorm')
|
58 |
| -fig.colorbar(im, extend='both', spacing='proportional', |
59 |
| - orientation='horizontal', shrink=0.8, ax=ax2) |
| 67 | +cbar = fig.colorbar(im, extend='both', spacing='proportional', |
| 68 | + shrink=0.9, ax=ax2) |
| 69 | +cbar.set_label('proportional') |
60 | 70 |
|
| 71 | +fig.suptitle('imshow, with out-of-range and masked data') |
61 | 72 | plt.show()
|
0 commit comments