matplotlib

Table Of Contents

Previous topic

matplotlib mlab

Next topic

matplotlib pyplot

This Page

Quick search

matplotlib path

matplotlib.path

Contains a class for managing paths (polylines).

class matplotlib.path.Path(vertices, codes=None)

Bases: object

Path represents a series of possibly disconnected, possibly closed, line and curve segments.

The underlying storage is made up of two parallel numpy arrays:
  • vertices: an Nx2 float array of vertices
  • codes: an N-length uint8 array of vertex types

These two arrays always have the same length in the first dimension. For example, to represent a cubic curve, you must provide three vertices as well as three codes CURVE3.

The code types are:

  • STOP : 1 vertex (ignored)

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

  • MOVETO : 1 vertex

    Pick up the pen and move to the given vertex.

  • LINETO : 1 vertex

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

  • CURVE3 : 1 control point, 1 endpoint

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

  • CURVE4 : 2 control points, 1 endpoint

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

  • CLOSEPOLY : 1 vertex (ignored)

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

Users of Path objects should not access the vertices and codes arrays directly. Instead, they should use iter_segments() to get the vertex/code pairs. This is important, since many Path objects, as an optimization, do not store a codes at all, but have a default one provided for them by iter_segments().

Note also that the vertices and codes arrays should be treated as immutable – there are a number of optimizations and assumptions made up front in the constructor that will not change when the data changes.

Create a new path with the given vertices and codes.

vertices is an Nx2 numpy float array, masked array or Python sequence.

codes is an N-length numpy array or Python sequence of type matplotlib.path.Path.code_type.

These two arrays must have the same length in the first dimension.

If codes is None, vertices will be treated as a series of line segments.

If vertices contains masked values, they will be converted to NaNs which are then handled correctly by the Agg PathIterator and other consumers of path data, such as iter_segments().

arc

(staticmethod) Returns an arc on the unit circle from angle theta1 to angle theta2 (in degrees).

If n is provided, it is the number of spline segments to make. If n is not provided, the number of spline segments is determined based on the delta between theta1 and theta2.

Masionobe, L. 2003. Drawing an elliptical arc using polylines, quadratic or cubic Bezier curves.
code_type()
contains_path(path, transform=None)

Returns True if this path completely contains the given path.

If transform is not None, the path will be transformed before performing the test.

contains_point(point, transform=None)

Returns True if the path contains the given point.

If transform is not None, the path will be transformed before performing the test.

get_extents(transform=None)

Returns the extents (xmin, ymin, xmax, ymax) of the path.

Unlike computing the extents on the vertices alone, this algorithm will take into account the curves and deal with control points appropriately.

interpolated(steps)
Returns a new path resampled to length N x steps. Does not currently handle interpolating curves.
intersects_bbox(bbox, filled=True)

Returns True if this path intersects a given Bbox.

filled, when True, treats the path as if it was filled. That is, if one path completely encloses the other, intersects_path() will return True.

intersects_path(other, filled=True)

Returns True if this path intersects another given path.

filled, when True, treats the paths as if they were filled. That is, if one path completely encloses the other, intersects_path() will return True.

iter_segments(simplify=None)

Iterates over all of the curve segments in the path. Each iteration returns a 2-tuple (vertices, code), where vertices is a sequence of 1 - 3 coordinate pairs, and code is one of the Path codes.

If simplify is provided, it must be a tuple (width, height) defining the size of the figure, in native units (e.g. pixels or points). Simplification implies both removing adjacent line segments that are very close to parallel, and removing line segments outside of the figure. The path will be simplified only if should_simplify is True, which is determined in the constructor by this criteria:

  • No curves
  • More than 128 vertices
make_compound_path
(staticmethod) Make a compound path from a list of Path objects. Only polygons (not curves) are supported.
to_polygons(transform=None, width=0, height=0)

Convert this path to a list of polygons. Each polygon is an Nx2 array of vertices. In other words, each polygon has no MOVETO instructions or curves. This is useful for displaying in backends that do not support compound paths or Bezier curves, such as GDK.

If width and height are both non-zero then the lines will be simplified so that vertices outside of (0, 0), (width, height) will be clipped.

transformed(transform)

Return a transformed copy of the path.

See also

matplotlib.transforms.TransformedPath: A specialized path class that will cache the transformed result and automatically update when the transform changes.

unit_circle

(staticmethod) Returns a Path of the unit circle. The circle is approximated using cubic Bezier curves. This uses 8 splines around the circle using the approach presented here:

Lancaster, Don. Approximating a Circle or an Ellipse Using Four Bezier Cubic Splines.
unit_rectangle
(staticmethod) Returns a Path of the unit rectangle from (0, 0) to (1, 1).
unit_regular_asterisk
(staticmethod) Returns a Path for a unit regular asterisk with the given numVertices and radius of 1.0, centered at (0, 0).
unit_regular_polygon
(staticmethod) Returns a Path for a unit regular polygon with the given numVertices and radius of 1.0, centered at (0, 0).
unit_regular_star
(staticmethod) Returns a Path for a unit regular star with the given numVertices and radius of 1.0, centered at (0, 0).
wedge

(staticmethod) Returns a wedge of the unit circle from angle theta1 to angle theta2 (in degrees).

If n is provided, it is the number of spline segments to make. If n is not provided, the number of spline segments is determined based on the delta between theta1 and theta2.

matplotlib.path.get_path_collection_extents(*args)
Given a sequence of Path objects, returns the bounding box that encapsulates all of them.