|
4 | 4 | The second subplot illustrates the use of BoundaryNorm to
|
5 | 5 | get a filled contour effect.
|
6 | 6 | """
|
7 |
| - |
| 7 | +from copy import copy |
8 | 8 | from numpy import ma
|
9 | 9 | import matplotlib.colors as colors
|
10 | 10 | import matplotlib.pyplot as plt
|
11 | 11 | import matplotlib.mlab as mlab
|
12 | 12 | import numpy as np
|
13 | 13 |
|
| 14 | +# compute some interesting data |
14 | 15 | delta = 0.025
|
15 | 16 | x = y = np.arange(-3.0, 3.0, delta)
|
16 | 17 | X, Y = np.meshgrid(x, y)
|
|
19 | 20 | Z = 10*(Z2 - Z1) # difference of Gaussians
|
20 | 21 |
|
21 | 22 | # Set up a colormap:
|
22 |
| -palette = plt.cm.gray |
| 23 | +# use copy so that we do not mutate the global colormap instance |
| 24 | +palette = copy(plt.cm.gray) |
23 | 25 | palette.set_over('r', 1.0)
|
24 | 26 | palette.set_under('g', 1.0)
|
25 | 27 | palette.set_bad('b', 1.0)
|
|
35 | 37 | # range to which the regular palette color scale is applied.
|
36 | 38 | # Anything above that range is colored based on palette.set_over, etc.
|
37 | 39 |
|
38 |
| -plt.subplot(1, 2, 1) |
39 |
| -im = plt.imshow(Zm, interpolation='bilinear', |
| 40 | +# set up the axes |
| 41 | +fig, (ax1, ax2) = plt.subplots(1, 2) |
| 42 | + |
| 43 | +# plot using 'continuous' color map |
| 44 | +im = ax1.imshow(Zm, interpolation='bilinear', |
40 | 45 | cmap=palette,
|
41 | 46 | norm=colors.Normalize(vmin=-1.0, vmax=1.0, clip=False),
|
42 | 47 | origin='lower', extent=[-3, 3, -3, 3])
|
43 |
| -plt.title('Green=low, Red=high, Blue=bad') |
44 |
| -plt.colorbar(im, extend='both', orientation='horizontal', shrink=0.8) |
| 48 | +ax1.set_title('Green=low, Red=high, Blue=bad') |
| 49 | +fig.colorbar(im, extend='both', orientation='horizontal', shrink=0.8, ax=ax1) |
45 | 50 |
|
46 |
| -plt.subplot(1, 2, 2) |
47 |
| -im = plt.imshow(Zm, interpolation='nearest', |
| 51 | +# plot using 'discrete' color map |
| 52 | +im = ax2.imshow(Zm, interpolation='nearest', |
48 | 53 | cmap=palette,
|
49 | 54 | norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1],
|
50 | 55 | ncolors=256, clip=False),
|
51 | 56 | origin='lower', extent=[-3, 3, -3, 3])
|
52 |
| -plt.title('With BoundaryNorm') |
53 |
| -plt.colorbar(im, extend='both', spacing='proportional', |
54 |
| - orientation='horizontal', shrink=0.8) |
| 57 | +ax2.set_title('With BoundaryNorm') |
| 58 | +fig.colorbar(im, extend='both', spacing='proportional', |
| 59 | + orientation='horizontal', shrink=0.8, ax=ax2) |
55 | 60 |
|
56 | 61 | plt.show()
|
0 commit comments