|
1 | 1 | import numpy as np |
2 | 2 | import matplotlib.pyplot as plt |
3 | 3 | from matplotlib.colors import LightSource |
| 4 | +from matplotlib import cbook |
4 | 5 |
|
5 | | -# example showing how to make shaded relief plots |
| 6 | +# Example showing how to make shaded relief plots |
6 | 7 | # like Mathematica |
7 | 8 | # (http://reference.wolfram.com/mathematica/ref/ReliefPlot.html) |
8 | 9 | # or Generic Mapping Tools |
9 | 10 | # (http://gmt.soest.hawaii.edu/gmt/doc/gmt/html/GMT_Docs/node145.html) |
10 | 11 |
|
11 | | -# test data |
12 | | -X,Y=np.mgrid[-5:5:0.05,-5:5:0.05] |
13 | | -Z=np.sqrt(X**2+Y**2)+np.sin(X**2+Y**2) |
14 | | -# create light source object. |
15 | | -ls = LightSource(azdeg=0,altdeg=65) |
16 | | -# shade data, creating an rgb array. |
17 | | -rgb = ls.shade(Z,plt.cm.copper) |
18 | | -# plot un-shaded and shaded images. |
19 | | -plt.figure(figsize=(12,5)) |
20 | | -plt.subplot(121) |
21 | | -plt.imshow(Z,cmap=plt.cm.copper) |
22 | | -plt.title('imshow') |
23 | | -plt.xticks([]); plt.yticks([]) |
24 | | -plt.subplot(122) |
25 | | -plt.imshow(rgb) |
26 | | -plt.title('imshow with shading') |
27 | | -plt.xticks([]); plt.yticks([]) |
28 | | -plt.show() |
| 12 | +def main(): |
| 13 | + # Test data |
| 14 | + x, y = np.mgrid[-5:5:0.05, -5:5:0.05] |
| 15 | + z = 5 * (np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)) |
| 16 | + fig = compare(z, plt.cm.copper) |
| 17 | + fig.suptitle('HSV Blending Looks Best with Smooth Surfaces', y=0.95) |
| 18 | + |
| 19 | + dem = np.load(cbook.get_sample_data('jacksboro_fault_dem.npz')) |
| 20 | + fig = compare(dem['elevation'], plt.cm.gist_earth, ve=0.05) |
| 21 | + fig.suptitle('Overlay Blending Looks Best with Rough Surfaces', y=0.95) |
| 22 | + |
| 23 | + plt.show() |
| 24 | + |
| 25 | +def compare(z, cmap, ve=1): |
| 26 | + # Create subplots and hide ticks |
| 27 | + fig, axes = plt.subplots(ncols=2, nrows=2) |
| 28 | + for ax in axes.flat: |
| 29 | + ax.set(xticks=[], yticks=[]) |
| 30 | + |
| 31 | + # Illuminate the scene from the northwest |
| 32 | + ls = LightSource(azdeg=315, altdeg=45) |
| 33 | + |
| 34 | + axes[0,0].imshow(z, cmap=cmap) |
| 35 | + axes[0,0].set(xlabel='Colormapped Data') |
| 36 | + |
| 37 | + axes[0,1].imshow(ls.hillshade(z, vert_exag=ve), cmap='gray') |
| 38 | + axes[0,1].set(xlabel='Illumination Intensity') |
| 39 | + |
| 40 | + axes[1,0].imshow(ls.shade(z, cmap=cmap, vert_exag=ve, blend_mode='hsv')) |
| 41 | + axes[1,0].set(xlabel='Blend Mode: "hsv" (default)') |
| 42 | + |
| 43 | + axes[1,1].imshow(ls.shade(z, cmap=cmap, vert_exag=ve, blend_mode='overlay')) |
| 44 | + axes[1,1].set(xlabel='Blend Mode: "overlay"') |
| 45 | + |
| 46 | + return fig |
| 47 | + |
| 48 | +if __name__ == '__main__': |
| 49 | + main() |
0 commit comments