8000 Merge pull request #10325 from anntzer/quadmesh-demo · matplotlib/matplotlib@6d5b1c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d5b1c4

Browse files
authored
Merge pull request #10325 from anntzer/quadmesh-demo
Minor improvements to quadmesh_demo.
2 parents 6839a6d + b465289 commit 6d5b1c4

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

examples/images_contours_and_fields/quadmesh_demo.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,38 @@
99
This demo illustrates a bug in quadmesh with masked data.
1010
"""
1111

12+
import copy
13+
14+
from matplotlib import cm, colors, pyplot as plt
1215
import numpy as np
13-
from matplotlib.pyplot import figure, show, savefig
14-
from matplotlib import cm, colors
15-
from numpy import ma
1616

1717
n = 12
1818
x = np.linspace(-1.5, 1.5, n)
1919
y = np.linspace(-1.5, 1.5, n * 2)
2020
X, Y = np.meshgrid(x, y)
2121
Qx = np.cos(Y) - np.cos(X)
2222
Qz = np.sin(Y) + np.sin(X)
23-
Qx = (Qx + 1.1)
2423
Z = np.sqrt(X**2 + Y**2) / 5
2524
Z = (Z - Z.min()) / (Z.max() - Z.min())
2625

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')
2932

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')
3439

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')
4343

44+
fig.tight_layout()
4445

45-
show()
46+
plt.show()

0 commit comments

Comments
 (0)
0