Custom Hillshading in a 3D Surface Plot

PythonPythonBeginner
Practice Now

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

Introduction

This lab demonstrates how to use custom hillshading in a 3D surface plot using Python Matplotlib. Hillshading is the use of light and shadow to enhance the perception of depth and relief in a 3D plot. By customizing the hillshading, we can create a more visually appealing and informative plot.

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/AdvancedPlottingGroup(["`Advanced Plotting`"]) 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/AdvancedPlottingGroup -.-> matplotlib/3d_plots("`3D Plots`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} matplotlib/importing_matplotlib -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} matplotlib/figures_axes -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} matplotlib/3d_plots -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/variables_data_types -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/booleans -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/lists -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/tuples -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/importing_modules -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/using_packages -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/data_collections -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/numerical_computing -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/data_visualization -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} python/build_in_functions -.-> lab-48644{{"`Custom Hillshading in a 3D Surface Plot`"}} end

Load and format data

In this step, we will load and format the data for the 3D surface plot. We will be using a sample dataset called "jacksboro_fault_dem.npz".

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cbook, cm
from matplotlib.colors import LightSource

## Load and format data
dem = cbook.get_sample_data('jacksboro_fault_dem.npz')
z = dem['elevation']
nrows, ncols = z.shape
x = np.linspace(dem['xmin'], dem['xmax'], ncols)
y = np.linspace(dem['ymin'], dem['ymax'], nrows)
x, y = np.meshgrid(x, y)

region = np.s_[5:50, 5:50]
x, y, z = x[region], y[region], z[region]

Set up plot

In this step, we will set up the plot for the 3D surface plot. We will be using a LightSource object to customize the hillshading.

## Set up plot
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))

ls = LightSource(270, 45)
## To use a custom hillshading mode, override the built-in shading and pass
## in the rgb colors of the shaded surface calculated from "shade".
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
                       linewidth=0, antialiased=False, shade=False)

plt.show()

Customize Hillshading

In this step, we will customize the hillshading by overriding the built-in shading and passing in the RGB colors of the shaded surface calculated from "shade".

## Set up plot
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))

ls = LightSource(270, 45)
## To use a custom hillshading mode, override the built-in shading and pass
## in the rgb colors of the shaded surface calculated from "shade".
rgb = ls.shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=rgb,
                       linewidth=0, antialiased=False, shade=False)

plt.show()

Review and revise

Review the code and make any necessary revisions. Ensure that the code is accurate and well-commented.

Summary

In this lab, we learned how to use custom hillshading in a 3D surface plot using Python Matplotlib. By customizing the hillshading, we were able to create a more visually appealing and informative plot.

Other Python Tutorials you may like