Creating a Contour MapΒΆ

Creating a contour map from a DEM mesh

# sphinx_gallery_thumbnail_number = 2
import pyvista as pv
from pyvista import examples
import numpy as np
import matplotlib.pyplot as plt
path, _ = examples.downloads._download_file("Sio020320.vtp")
mesh = pv.read(path).elevation()
mesh
HeaderData Arrays
PolyDataInformation
N Cells203683
N Points610926
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

Out:

(5100.0, 5400.0)

create values to contour at

step = 10
cntrs = np.arange(mi, ma + step, step)
cntrs

Out:

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

Out:

[(469978.85959530954, 1322266.5229163894, 5293.630545897349),
 (469956.03091019404, 1321834.8498636105, 5194.622946764996),
 (0.0037194010444579987, -0.2237385074834842, 0.9746421119184896)]

Create a 2D contour map

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

plt.scatter(x, y, s=1, marker=".")
contouring

Out:

<matplotlib.collections.PathCollection object at 0x7f203d3a8790>

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

Gallery generated by Sphinx-Gallery