Creating Shaded Relief Plots with Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to create shaded relief plots using Python Matplotlib. Shaded relief plots are useful for visualizing terrain data, as they use shading to represent variations in elevation.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("`Heatmaps`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} matplotlib/importing_matplotlib -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} matplotlib/figures_axes -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} matplotlib/heatmaps -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/variables_data_types -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/for_loops -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/lists -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/tuples -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/importing_modules -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/using_packages -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/data_collections -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/numerical_computing -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/data_visualization -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} python/build_in_functions -.-> lab-48924{{"`Creating Shaded Relief Plots with Python`"}} end

Import Libraries

We will start by importing the necessary libraries.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib import cbook
from matplotlib.colors import LightSource

Load Data

Next, we will load the sample data that we will use for this tutorial. We will use the jacksboro_fault_dem.npz file, which contains elevation data.

dem = cbook.get_sample_data('jacksboro_fault_dem.npz')
elev = dem['elevation']

Create Shaded Relief Plots

We will now create the shaded relief plots using the LightSource class. We will create two subplots, one with a colormapped data and the other with illumination intensity.

## Illuminate the scene from the northwest
ls = LightSource(azdeg=315, altdeg=45)

fig, axs = plt.subplots(ncols=2, nrows=2)
for ax in axs.flat:
    ax.set(xticks=[], yticks=[])

axs[0, 0].imshow(z, cmap=cmap)
axs[0, 0].set(xlabel='Colormapped Data')

axs[0, 1].imshow(ls.hillshade(z, vert_exag=ve), cmap='gray')
axs[0, 1].set(xlabel='Illumination Intensity')

We will create two more subplots, one with the blend_mode set to "hsv" and the other set to "overlay".

rgb = ls.shade(z, cmap=cmap, vert_exag=ve, blend_mode='hsv')
axs[1, 0].imshow(rgb)
axs[1, 0].set(xlabel='Blend Mode: "hsv" (default)')

rgb = ls.shade(z, cmap=cmap, vert_exag=ve, blend_mode='overlay')
axs[1, 1].imshow(rgb)
axs[1, 1].set(xlabel='Blend Mode: "overlay"')

Display the Plots

Finally, we will display the plots using plt.show().

plt.show()

Summary

In this lab, we learned how to create shaded relief plots using Python Matplotlib. We loaded sample data and used the LightSource class to create four subplots with different shading techniques. We then displayed the plots using plt.show().

Other Python Tutorials you may like