ContourPy: Contour Calculation for 2D Quadrilateral Grids
ContourPy is a Python library for calculating contours of 2D quadrilateral grids, written in C++11 and wrapped using pybind11. It offers algorithms used in Matplotlib, including the 2005 and 2014 versions, as well as a newer algorithm with additional features available in both serial and multithreaded versions. The current version is 1.3.3, released on March 7, 2026, with a release cadence of approximately every 2-3 months.
Warnings
- breaking Support for Python versions below 3.11 has been dropped in version 1.3.3.
- deprecated The multithreaded option in the newer algorithm is considered experimental.
Install
-
pip install contourpy
Imports
- contour_generator
from contourpy import contour_generator
Quickstart
import numpy as np
from contourpy import contour_generator
# Create a 2x2 grid of z values
z = np.array([[0.0, 0.1], [0.2, 0.3]])
# Initialize the contour generator
cont_gen = contour_generator(z=z)
# Calculate contour lines at z-level 0.25
lines = cont_gen.lines(0.25)
# Calculate filled contours between z-levels 0.15 and 0.25
filled = cont_gen.filled(0.15, 0.25)
# Visualize the contours using Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for line in lines:
ax.plot(line[:, 0], line[:, 1], color='red')
for polygon in filled[0]:
ax.fill(polygon[:, 0], polygon[:, 1], color='gold', alpha=0.5)
plt.show()