Skip to content

Commit f231e2c

Browse files
greglucasanntzer
authored andcommitted
Demonstrate alpha ramp in colormap in quadmesh example. (#15)
1 parent ece69da commit f231e2c

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

examples/quadmesh.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# Combination of quadmesh_demo and test_axes.test_pcolormesh.
1+
# Modified from quadmesh_demo and test_axes.test_pcolormesh.
22

33
from matplotlib import pyplot as plt
4+
from matplotlib.colors import ListedColormap
45
import numpy as np
56

67
n = 12
@@ -16,13 +17,22 @@
1617
# The color array can include masked values:
1718
Zm = np.ma.masked_where(np.abs(Qz) < 0.5 * np.max(Qz), Z)
1819

19-
fig, axs = plt.subplots(2, 2)
20-
axs[0, 0].pcolormesh(Qx, Qz, Z, shading='gouraud')
21-
axs[0, 1].pcolormesh(Qx, Qz, Zm, shading='gouraud')
22-
axs[1, 0].pcolormesh(Qx, Qz, Z, lw=0.5, edgecolors='k')
23-
axs[1, 1].pcolormesh(Qx, Qz, Z, lw=2, edgecolors=['b', 'w'])
20+
# Set up the colormaps
21+
cmap = plt.get_cmap('viridis')
22+
cmap_data = cmap(np.arange(cmap.N))
23+
# Set a linear ramp in alpha
24+
cmap_data[:,-1] = np.linspace(0.2, 0.8, cmap.N)
25+
# Create new colormap
26+
cmap_alpha = ListedColormap(cmap_data)
27+
28+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
29+
axs[0, 0].pcolormesh(Qx, Qz, Z, cmap=cmap_alpha, shading='gouraud')
30+
axs[0, 1].pcolormesh(Qx, Qz, Zm, cmap=cmap_alpha, shading='gouraud')
31+
axs[1, 0].pcolormesh(Qx, Qz, Z, cmap=cmap_alpha, edgecolors='k')
32+
mesh = axs[1, 1].pcolormesh(Qx, Qz, Zm, cmap=cmap_alpha)
33+
fig.colorbar(mesh, ax=axs[:, 1], orientation='vertical')
34+
2435
for ax in axs.flat:
2536
ax.set_axis_off()
26-
fig.tight_layout()
2737

2838
plt.show()

0 commit comments

Comments
 (0)