Skip to content

Instantly share code, notes, and snippets.

@robbibt
Last active July 29, 2021 08:15
Show Gist options
  • Select an option

  • Save robbibt/6c42253b803089c713d21d50d0e6fe15 to your computer and use it in GitHub Desktop.

Select an option

Save robbibt/6c42253b803089c713d21d50d0e6fe15 to your computer and use it in GitHub Desktop.
Applying a hillshade to an array using Matplotlib
# 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')
@alexgleith
Copy link
Copy Markdown

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment