Python library for calculating contours of 2D quadrilateral grids
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for creating contour generators and computing contours. The contour_generator() function is the main factory function that creates appropriate algorithm instances based on the specified parameters.
Creates and returns a ContourGenerator object with the class and properties determined by the function arguments.
def contour_generator(
x: ArrayLike | None = None,
y: ArrayLike | None = None,
z: ArrayLike | np.ma.MaskedArray | None = None,
*,
name: str = "serial",
corner_mask: bool | None = None,
line_type: LineType | str | None = None,
fill_type: FillType | str | None = None,
chunk_size: int | tuple[int, int] | None = None,
chunk_count: int | tuple[int, int] | None = None,
total_chunk_count: int | None = None,
quad_as_tri: bool = False,
z_interp: ZInterp | str | None = ZInterp.Linear,
thread_count: int = 0,
) -> ContourGenerator:
"""
Create and return a ContourGenerator object.
Parameters:
- x: array-like of shape (ny, nx) or (nx,), optional - X-coordinates of z values
- y: array-like of shape (ny, nx) or (ny,), optional - Y-coordinates of z values
- z: array-like of shape (ny, nx) - 2D gridded values to calculate contours of
- name: str - Algorithm name ("serial", "threaded", "mpl2005", "mpl2014")
- corner_mask: bool, optional - Enable/disable corner masking for masked arrays
- line_type: LineType or str, optional - Format of contour line data
- fill_type: FillType or str, optional - Format of filled contour data
- chunk_size: int or tuple(int, int), optional - Chunk size in (y, x) directions
- chunk_count: int or tuple(int, int), optional - Chunk count in (y, x) directions
- total_chunk_count: int, optional - Total number of chunks
- quad_as_tri: bool - Treat quads as 4 triangles (default False)
- z_interp: ZInterp or str, optional - Z value interpolation method
- thread_count: int - Number of threads for threaded algorithm (default 0)
Returns:
ContourGenerator instance configured according to parameters
"""import contourpy
import numpy as np
# Create sample data
x = np.linspace(0, 10, 50)
y = np.linspace(0, 10, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
# Create contour generator with default settings
cont_gen = contourpy.contour_generator(X, Y, Z)
# Generate contour lines
lines = cont_gen.lines(0.5)# Use threaded algorithm for better performance
cont_gen = contourpy.contour_generator(X, Y, Z, name="threaded", thread_count=4)
# Use legacy Matplotlib 2014 algorithm
cont_gen = contourpy.contour_generator(X, Y, Z, name="mpl2014")# Configure output formats
cont_gen = contourpy.contour_generator(
X, Y, Z,
line_type="SeparateCode", # Include path codes with lines
fill_type="OuterOffset" # Use offset format for filled contours
)# Create masked array
Z_masked = np.ma.masked_where(Z < 0.2, Z)
# Enable corner masking for better masked contours
cont_gen = contourpy.contour_generator(X, Y, Z_masked, corner_mask=True)# Use chunking for memory efficiency
cont_gen = contourpy.contour_generator(
X, Y, Z,
chunk_size=(20, 20), # Process in 20x20 chunks
name="threaded", # Use threaded algorithm
thread_count=2 # Use 2 threads
)Four algorithms are available:
Control memory usage and enable parallelization:
Only one of these options should be specified at a time.
Control how Z values are interpolated:
For the "threaded" algorithm:
Install with Tessl CLI
npx tessl i tessl/pypi-contourpy