core Module

Common enums, functions, and classes for the contours package.

Summary

MPLPATHCODE Matplotlib path codes.
numpy_formatter NumPy style contour formatter.
matlab_formatter MATLAB style contour formatter.
shapely_formatter Shapely style contour formatter.
ContourMixin Mixin to provide the public contour methods.

Enumerations

class contours.core.MPLPATHCODE[source]

Matplotlib path codes.

These are included in this library for the purpose of writting your own formatter. The description of each one is copied verbatim from the documentation for matplotlib.path.Path.

CLOSEPOLY = 79

1 vertex (ignored)

Draw a line segment to the start point of the current polyline.

CURVE3 = 3

1 control point, 1 endpoint

Draw a quadratic Bezier curve from he current position, with the given control point, to the given end point.

CURVE4 = 4

2 control points, 1 endpoint

Draw a cubic Bezier curve from the current position, with the given control points, to the given and point.

LINETO = 2

1 vertex

Draw a line from the current position to the given vertex.

MOVETO = 1

1 vertex

Pick up the pen and move to the given vertex.

STOP = 0

1 vertex (ignored)

A marker fo the end of the entire path (currently not required and ignored).

Formatters

Contour formatters are meant to be given as the formatter argument when constructing a contour generator. The default formatter is numpy_formatter().

contours.core.numpy_formatter()[source]

NumPy style contour formatter.

Contours are returned as a list of Nx2 arrays containing the x and y vertices of the contour line.

For filled contours the direction of vertices matters:

  • CCW (ACW): The vertices give the exterior of a contour polygon.
  • CW: The vertices give a hole of a contour polygon. This hole will
    always be inside the exterior of the last contour exterior.

Note

This is the fastest format.

contours.core.matlab_formatter()[source]

MATLAB style contour formatter.

Contours are returned as a single Nx2, MATLAB style, contour array. There are two types of rows in this format:

  • Header: The first element of a header row is the level of the contour (the lower level for filled contours) and the second element is the number of vertices (to follow) belonging to this contour line.
  • Vertex: x,y coordinate pairs of the vertex.

A header row is always followed by the coresponding number of vertices. Another header row may follow if there are more contour lines.

For filled contours the direction of vertices matters:

  • CCW (ACW): The vertices give the exterior of a contour polygon.
  • CW: The vertices give a hole of a contour polygon. This hole will
    always be inside the exterior of the last contour exterior.

For further explanation of this format see the Mathworks documentation noting that the MATLAB format used in the contours package is the transpose of that used by MATLAB (since MATLAB is column-major and NumPy is row-major by default).

contours.core.shapely_formatter()[source]

Shapely style contour formatter.

Contours are returned as a list of shapely.geometry.LineString, shapely.geometry.LinearRing, and shapely.geometry.Point geometry elements.

Filled contours return a list of shapely.geometry.Polygon elements instead.

Note

If possible, Shapely speedups will be enabled.

See also

descartes : Use Shapely or GeoJSON-like geometric objects as matplotlib paths and patches.

Writting your own formatter.

Formatter functions follow the general format below.

contours.core.sample_formatter(level, vertices, codes=None)

Note

The sample_formatter() does not have an implementation.

Parameters:
  • level (numbers.Number) – The level of the contour or a tuple giving the (min, max) for filled contours.
  • vertices (list(numpy.ndarray)) – A list of arrays where each array is Nx2 and gives the vertices of a contour. If the vertices are for a filled contour the vertices also give any holes and codes will be given. Exteriors are always CCW and interiors are always CW.
  • codes (list(numpy.ndarray)) – A list of arrays contiaining MPLPATHCODE drawing codes. Each polyline in each array begins with MPLPATHCODE.MOVETO and ends with MPLPATHCODE.CLOSEPOLY. Each array can contain multiple polylines, the first gives the exterior of the polygon while the rest give the holes. Only given for filled contours.
Returns:

This is implementation dependent but the general rule is that is should represent the contour or filled contour in some way.

Classes

Inheritance diagram of ContourMixin

class contours.core.ContourMixin(formatter=<function numpy_formatter>, *args, **kwargs)[source]

Bases: object

Mixin to provide the public contour methods.

Parameters:formatter (callable) – A conversion function to convert from the internal Matplotlib contour format to an external format. See Formatters for more information.
formatter

callable() – A conversion function to convert from the internal Matplotlib contour format to an external format. See Formatters for more information.

Methods:
contour(level) Get contour lines at the given level.
filled_contour([min, max]) Get contour polygons between the given levels.
contour(level)[source]

Get contour lines at the given level.

Parameters:level (numbers.Number) – The data level to calculate the contour lines for.
Returns:The result of the formatter called on the contour at the given level.
filled_contour(min=None, max=None)[source]

Get contour polygons between the given levels.

Parameters:
  • min (numbers.Number or None) – The minimum data level of the contour polygon. If None, numpy.finfo(numpy.float64).min will be used.
  • max (numbers.Number or None) – The maximum data level of the contour polygon. If None, numpy.finfo(numpy.float64).max will be used.
Returns:

The result of the formatter called on the filled contour between min and max.