Last active
July 29, 2021 08:15
-
-
Save robbibt/6c42253b803089c713d21d50d0e6fe15 to your computer and use it in GitHub Desktop.
Applying a hillshade to an array using Matplotlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code via Alex Leith | |
| from matplotlib.colors import LightSource | |
| import matplotlib.pyplot as plt | |
| # Sample elevation array | |
| elevation = data_cube.elevation.isel(time=0).values | |
| # Create hillsahde based on the array | |
| ls = LightSource(azdeg=315, altdeg=45) | |
| dyx = 10 | |
| shade = ls.hillshade(elevation, vert_exag=0.5, dx=dyx, dy=dyx, fraction=1.0) | |
| # Modify the original elevation data to include shade | |
| data_cube['shaded'] = (['latitude', 'longitude'], elevation * shade) | |
| # Plot using bilinear interpolation to remove artefacts | |
| data_cube.shaded.plot(size=10, interpolation='bilinear') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!