|
18 | 18 | y, x = np.mgrid[slice(1, 5 + dy, dy),
|
19 | 19 | slice(1, 5 + dx, dx)]
|
20 | 20 |
|
21 |
| -z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x) |
| 21 | +z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x) |
22 | 22 |
|
23 | 23 | # x and y are bounds, so z should be the value *inside* those bounds.
|
24 | 24 | # Therefore, remove the last value from the z array.
|
|
31 | 31 | cmap = plt.get_cmap('PiYG')
|
32 | 32 | norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
|
33 | 33 |
|
34 |
| -plt.subplot(2, 1, 1) |
35 |
| -im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm) |
36 |
| -plt.colorbar() |
37 |
| -# set the limits of the plot to the limits of the data |
38 |
| -plt.axis([x.min(), x.max(), y.min(), y.max()]) |
39 |
| -plt.title('pcolormesh with levels') |
| 34 | +fig, (ax0, ax1) = plt.subplots(nrows=2) |
| 35 | + |
| 36 | +im = ax0.pcolormesh(x, y, z, cmap=cmap, norm=norm) |
| 37 | +fig.colorbar(im, ax=ax0) |
| 38 | +ax0.set_title('pcolormesh with levels') |
40 | 39 |
|
41 | 40 |
|
42 |
| -plt.subplot(2, 1, 2) |
43 | 41 | # contours are *point* based plots, so convert our bound into point
|
44 | 42 | # centers
|
45 |
| -plt.contourf(x[:-1, :-1] + dx / 2., |
46 |
| - y[:-1, :-1] + dy / 2., z, levels=levels, |
47 |
| - cmap=cmap) |
48 |
| -plt.colorbar() |
49 |
| -plt.title('contourf with levels') |
50 |
| - |
| 43 | +cf = ax1.contourf(x[:-1, :-1] + dx/2., |
| 44 | + y[:-1, :-1] + dy/2., z, levels=levels, |
| 45 | + cmap=cmap) |
| 46 | +fig.colorbar(cf, ax=ax1) |
| 47 | +ax1.set_title('contourf with levels') |
| 48 | + |
| 49 | +# adjust spacing between subplots so `ax1` title and `ax0` tick labels |
| 50 | +# don't overlap |
| 51 | +fig.tight_layout() |
51 | 52 |
|
52 | 53 | plt.show()
|
0 commit comments