path

matplotlib.path

A module for dealing with the polylines used throughout matplotlib.

The primary class for polyline handling in matplotlib is Path. Almost all vector drawing makes use of Paths somewhere in the drawing pipeline.

Whilst a Path instance itself cannot be drawn, there exists Artist subclasses which can be used for convenient Path visualisation - the two most frequently used of these are PathPatch and PathCollection.

class matplotlib.path.Path(vertices, codes=None, _interpolation_steps=1, closed=False, readonly=False)

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() or cleaned() 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

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.

Parameters:

vertices : array_like

The (n, 2) float array, masked array or sequence of pairs representing the vertices of the path.

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().

codes : {None, array_like}, optional

n-length array integers representing the codes of the path. If not None, codes must be the same length as vertices. If None, vertices will be treated as a series of line segments.

_interpolation_steps : int, optional

Used as a hint to certain projections, such as Polar, that this path should be linearly interpolated immediately before drawing. This attribute is primarily an implementation detail and is not intended for public use.

closed : bool, optional

If codes is None and closed is True, vertices will be treated as line segments of a closed polygon.

readonly : bool, optional

Makes the path behave in an immutable way and sets the vertices and codes as read-only arrays.

CLOSEPOLY = 79
CURVE3 = 3
CURVE4 = 4
LINETO = 2
MOVETO = 1
NUM_VERTICES_FOR_CODE = {0: 1, 1: 1, 2: 1, 3: 2, 4: 3, 79: 1}

A dictionary mapping Path codes to the number of vertices that the code expects.

STOP = 0
classmethod arc(theta1, theta2, n=None, is_wedge=False)

Return 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.

cleaned(transform=None, remove_nans=False, clip=None, quantize=False, simplify=False, curves=False, stroke_width=1.0, snap=False, sketch=None)

Cleans up the path according to the parameters returning a new Path instance.

See also

See iter_segments() for details of the keyword arguments.

Returns:Path instance with cleaned up vertices and codes. :
clip_to_bbox(bbox, inside=True)

Clip the path to the given bounding box.

The path must be made up of one or more closed polygons. This algorithm will not behave correctly for unclosed paths.

If inside is True, clip to the inside of the box, otherwise to the outside of the box.

code_type

alias of uint8

codes

The list of codes in the Path as a 1-D numpy array. Each code is one of STOP, MOVETO, LINETO, CURVE3, CURVE4 or CLOSEPOLY. For codes that correspond to more than one vertex (CURVE3 and CURVE4), that code will be repeated so that the length of self.vertices and self.codes is always the same.

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, radius=0.0)

Returns True if the path contains the given point.

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

radius allows the path to be made slightly larger or smaller.

contains_points(points, transform=None, radius=0.0)

Returns a bool array which is True if the path contains the corresponding point.

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

radius allows the path to be made slightly larger or smaller.

copy()

Returns a shallow copy of the Path, which will share the vertices and codes with the source Path.

deepcopy()

Returns a deepcopy of the Path. The Path will not be readonly, even if the source Path is.

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.

has_nonfinite

True if the vertices array has nonfinite values.

classmethod hatch(hatchpattern, density=6)

Given a hatch specifier, hatchpattern, generates a Path that can be used in a repeated hatching pattern. density is the number of lines per unit square.

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(transform=None, remove_nans=True, clip=None, snap=False, stroke_width=1.0, simplify=None, curves=True, sketch=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.

Additionally, this method can provide a number of standard cleanups and conversions to the path.

Parameters:

transform : None or Transform instance

If not None, the given affine transformation will be applied to the path.

remove_nans : {False, True}, optional

If True, will remove all NaNs from the path and insert MOVETO commands to skip over them.

clip : None or sequence, optional

If not None, must be a four-tuple (x1, y1, x2, y2) defining a rectangle in which to clip the path.

snap : None or bool, optional

If None, auto-snap to pixels, to reduce fuzziness of rectilinear lines. If True, force snapping, and if False, don’t snap.

stroke_width : float, optional

The width of the stroke being drawn. Needed

as a hint for the snapping algorithm.

simplify : None or bool, optional

If True, perform simplification, to remove

vertices that do not affect the appearance of the path. If False, perform no simplification. If None, use the should_simplify member variable.

curves : {True, False}, optional

If True, curve segments will be returned as curve segments. If False, all curves will be converted to line segments.

sketch : None or sequence, optional

If not None, must be a 3-tuple of the form (scale, length, randomness), representing the sketch parameters.

classmethod make_compound_path(*args)

Make a compound path from a list of Path objects.

classmethod make_compound_path_from_polys(XY)

Make a compound path object to draw a number of polygons with equal numbers of sides XY is a (numpolys x numsides x 2) numpy array of vertices. Return object is a Path

(Source code, png)

../_images/histogram_path_demo.png
readonly

True if the Path is read-only.

should_simplify

True if the vertices array should be simplified.

simplify_threshold

The fraction of a pixel difference below which vertices will be simplified out.

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.
classmethod unit_circle()

Return 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:

classmethod unit_circle_righthalf()

Return a Path of the right half of a unit circle. The circle is approximated using cubic Bezier curves. This uses 4 splines around the circle using the approach presented here:

classmethod unit_rectangle()

Return a Path instance of the unit rectangle from (0, 0) to (1, 1).

classmethod unit_regular_asterisk(numVertices)

Return a Path for a unit regular asterisk with the given numVertices and radius of 1.0, centered at (0, 0).

classmethod unit_regular_polygon(numVertices)

Return a Path instance for a unit regular polygon with the given numVertices and radius of 1.0, centered at (0, 0).

classmethod unit_regular_star(numVertices, innerCircle=0.5)

Return a Path for a unit regular star with the given numVertices and radius of 1.0, centered at (0, 0).

vertices

The list of vertices in the Path as an Nx2 numpy array.

classmethod wedge(theta1, theta2, n=None)

Return 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.cleanup_path(*args, **kwargs)

Deprecated since version 1.3: The cleanup_path function was deprecated in version 1.3. Use path.Path.cleaned instead.

cleanup_path(path, trans, remove_nans, clip, snap, simplify, curves, sketch_params)

matplotlib.path.clip_path_to_rect(*args, **kwargs)

Deprecated since version 1.3: The clip_path_to_rect function was deprecated in version 1.3. Use path.Path.clip_to_bbox instead.

clip_path_to_rect(path, bbox, inside)

matplotlib.path.convert_path_to_polygons(*args, **kwargs)

Deprecated since version 1.3: The convert_path_to_polygons function was deprecated in version 1.3. Use path.Path.to_polygons instead.

convert_path_to_polygons(path, trans, width, height)

matplotlib.path.get_path_collection_extents(master_transform, paths, transforms, offsets, offset_transform)

Given a sequence of Path objects, Transform objects and offsets, as found in a PathCollection, returns the bounding box that encapsulates all of them.

master_transform is a global transformation to apply to all paths

paths is a sequence of Path instances.

transforms is a sequence of Affine2D instances.

offsets is a sequence of (x, y) offsets (or an Nx2 array)

offset_transform is a Affine2D to apply to the offsets before applying the offset to the path.

The way that paths, transforms and offsets are combined follows the same method as for collections. Each is iterated over independently, so if you have 3 paths, 2 transforms and 1 offset, their combinations are as follows:

(A, A, A), (B, B, A), (C, A, A)
matplotlib.path.get_path_extents(*args, **kwargs)

Deprecated since version 1.3: The get_path_extents function was deprecated in version 1.3. Use path.Path.get_extents instead.

get_path_extents(path, trans)

matplotlib.path.get_paths_extents(paths, transforms=[])

Given a sequence of Path objects and optional Transform objects, returns the bounding box that encapsulates all of them.

paths is a sequence of Path instances.

transforms is an optional sequence of Affine2D instances to apply to each path.

matplotlib.path.path_in_path(*args, **kwargs)

Deprecated since version 1.3: The path_in_path function was deprecated in version 1.3. Use path.Path.contains_path instead.

path_in_path(a, atrans, b, btrans)

matplotlib.path.path_intersects_path(*args, **kwargs)

Deprecated since version 1.3: The path_intersects_path function was deprecated in version 1.3. Use path.Path.intersects_path instead.

path_intersects_path(p1, p2)

matplotlib.path.point_in_path(*args, **kwargs)

Deprecated since version 1.3: The point_in_path function was deprecated in version 1.3. Use path.Path.contains_point instead.

point_in_path(x, y, path, trans)

matplotlib.path.point_in_path_collection(*args, **kwargs)

Deprecated since version 1.3: The point_in_path_collection function was deprecated in version 1.3. Use collection.Collection.contains instead.

point_in_path_collection(x, y, r, trans, paths, transforms, offsets, offsetTrans, filled)

matplotlib.path.points_in_path(*args, **kwargs)

Deprecated since version 1.3: The points_in_path function was deprecated in version 1.3. Use path.Path.contains_points instead.

points_in_path(points, path, trans)