Creating a Contour Map

Creating a contour map from a DEM mesh

# sphinx_gallery_thumbnail_number = 2
import matplotlib.pyplot as plt
import numpy as np
import pooch
import pyvista as pv
url = "https://raw.githubusercontent.com/pyvista/vtk-data/master/Data/Sio020320.vtp"
file_path = pooch.retrieve(url=url, known_hash=None)
mesh = pv.read(file_path).elevation()
mesh
Downloading data from 'https://raw.githubusercontent.com/pyvista/vtk-data/master/Data/Sio020320.vtp' to file '/home/runner/.cache/pooch/c0c6a619beaa06db63dfe7593cb5578f-Sio020320.vtp'.
SHA256 hash of downloaded file: eb4df0c0e1925f972023f798d81fd778027985ad9c25a2c57b49242fbeb74b16
Use this value as the 'known_hash' argument of 'pooch.retrieve' to ensure that the file hasn't changed if it is downloaded again in the future.
HeaderData Arrays
PolyDataInformation
N Cells203683
N Points610926
N Strips0
X Bounds4.694e+05, 4.702e+05
Y Bounds1.321e+06, 1.323e+06
Z Bounds5.120e+03, 5.368e+03
N Arrays1
NameFieldTypeN CompMinMax
ElevationPointsfloat3215.120e+035.368e+03


Get a min/max range of the elevation

z = mesh["Elevation"]
mi, ma = round(min(z), ndigits=-2), round(max(z), ndigits=-2)
mi, ma
(5100.0, 5400.0)

create values to contour at

step = 10
cntrs = np.arange(mi, ma + step, step)
cntrs
array([5100., 5110., 5120., 5130., 5140., 5150., 5160., 5170., 5180.,
       5190., 5200., 5210., 5220., 5230., 5240., 5250., 5260., 5270.,
       5280., 5290., 5300., 5310., 5320., 5330., 5340., 5350., 5360.,
       5370., 5380., 5390., 5400.])

Run the contouring filter

contours = mesh.contour(cntrs, scalars="Elevation")
p = pv.Plotter()
p.add_mesh(mesh)
p.add_mesh(contours, line_width=5, color="black")
p.camera_position = [
    (469978.85959530954, 1322266.5229163894, 5293.630545897349),
    (469956.03091019404, 1321834.8498636105, 5194.622946764996),
    (0.0037194010444579987, -0.2237385074834842, 0.9746421119184896),
]
p.show()
contouring

Create a 2D contour map

x, y = contours.points[:, 0], contours.points[:, 1]

plt.scatter(x, y, s=1, marker=".")
contouring
<matplotlib.collections.PathCollection object at 0x7f2f5f1aefd0>

Total running time of the script: (0 minutes 5.023 seconds)

Gallery generated by Sphinx-Gallery