Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31a4239

Browse files
committed
Added example of using the custom hillshading on a 3D surface plot
1 parent 51e9a6c commit 31a4239

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Demonstrates using custom hillshading in a 3D surface plot.
3+
"""
4+
from mpl_toolkits.mplot3d import Axes3D
5+
from matplotlib import cbook
6+
from matplotlib import cm
7+
from matplotlib.colors import LightSource
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
dem = np.load(cbook.get_sample_data('jacksboro_fault_dem.npz'))
12+
z = dem['elevation']
13+
nrows, ncols = z.shape
14+
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
15+
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
16+
x, y = np.meshgrid(x, y)
17+
18+
region = np.s_[5:50, 5:50]
19+
x, y, z = x[region], y[region], z[region]
20+
21+
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
22+
23+
ls = LightSource(270, 45)
24+
# To use a custom hillshading mode, override the built-in shading and pass
25+
# in the rgb colors of the shaded surface calculated from "shade".
26+
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
27+
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
28+
linewidth=0, antialiased=False, shade=False)
29+
30+
plt.show()
31+

0 commit comments

Comments
 (0)