|
9 | 9 | This demo illustrates a bug in quadmesh with masked data. |
10 | 10 | """ |
11 | 11 |
|
| 12 | +import copy |
| 13 | + |
| 14 | +from matplotlib import cm, colors, pyplot as plt |
12 | 15 | import numpy as np |
13 | | -from matplotlib.pyplot import figure, show, savefig |
14 | | -from matplotlib import cm, colors |
15 | | -from numpy import ma |
16 | 16 |
|
17 | 17 | n = 12 |
18 | 18 | x = np.linspace(-1.5, 1.5, n) |
19 | 19 | y = np.linspace(-1.5, 1.5, n * 2) |
20 | 20 | X, Y = np.meshgrid(x, y) |
21 | 21 | Qx = np.cos(Y) - np.cos(X) |
22 | 22 | Qz = np.sin(Y) + np.sin(X) |
23 | | -Qx = (Qx + 1.1) |
24 | 23 | Z = np.sqrt(X**2 + Y**2) / 5 |
25 | 24 | Z = (Z - Z.min()) / (Z.max() - Z.min()) |
26 | 25 |
|
27 | | -# The color array can include masked values: |
28 | | -Zm = ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z) |
| 26 | +# The color array can include masked values. |
| 27 | +Zm = np.ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z) |
| 28 | + |
| 29 | +fig, axs = plt.subplots(1, 3) |
| 30 | +axs[0].pcolormesh(Qx, Qz, Z, shading='gouraud') |
| 31 | +axs[0].set_title('Without masked values') |
29 | 32 |
|
30 | | -fig = figure() |
31 | | -ax = fig.add_subplot(121) |
32 | | -ax.pcolormesh(Qx, Qz, Z, shading='gouraud') |
33 | | -ax.set_title('Without masked values') |
| 33 | +# You can control the color of the masked region. We copy the default colormap |
| 34 | +# before modifying it. |
| 35 | +cmap = copy.copy(cm.get_cmap(plt.rcParams['image.cmap'])) |
| 36 | +cmap.set_bad('y', 1.0) |
| 37 | +axs[1].pcolormesh(Qx, Qz, Zm, shading='gouraud', cmap=cmap) |
| 38 | +axs[1].set_title('With masked values') |
34 | 39 |
|
35 | | -ax = fig.add_subplot(122) |
36 | | -# You can control the color of the masked region: |
37 | | -# cmap = cm.RdBu |
38 | | -# cmap.set_bad('y', 1.0) |
39 | | -# ax.pcolormesh(Qx, Qz, Zm, cmap=cmap) |
40 | | -# Or use the default, which is transparent: |
41 | | -col = ax.pcolormesh(Qx, Qz, Zm, shading='gouraud') |
42 | | -ax.set_title('With masked values') |
| 40 | +# Or use the default, which is transparent. |
| 41 | +axs[2].pcolormesh(Qx, Qz, Zm, shading='gouraud') |
| 42 | +axs[2].set_title('With masked values') |
43 | 43 |
|
| 44 | +fig.tight_layout() |
44 | 45 |
|
45 | | -show() |
| 46 | +plt.show() |
0 commit comments