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 Path
s somewhere in the drawing pipeline.
Whilst a Path
instance itself cannot be drawn, some Artist
subclasses,
such as PathPatch
and PathCollection
, can be used for convenient Path
visualisation.

class
matplotlib.path.
Path
(vertices, codes=None, _interpolation_steps=1, closed=False, readonly=False)[source]¶ Bases:
object
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 Nlength uint8 array of vertex types, or None
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.
If codes is None, it is interpreted as a
MOVETO
followed by a series ofLINETO
.Users of Path objects should not access the vertices and codes arrays directly. Instead, they should use
iter_segments
orcleaned
to get the vertex/code pairs. This helps, in particular, to consistently handle the case of codes being None.Some behavior of Path objects can be controlled by rcParams. See the rcParams whose keys start with 'path.'.
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:  verticesarraylike
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()
. codesarraylike or None, optional
nlength 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_stepsint, 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.
 closedbool, optional
If codes is None and closed is True, vertices will be treated as line segments of a closed polygon. Note that the last vertex will then be ignored (as the corresponding code will be set to CLOSEPOLY).
 readonlybool, optional
Makes the path behave in an immutable way and sets the vertices and codes as readonly 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)[source]¶ Return the unit circle arc from angles theta1 to theta2 (in degrees).
theta2 is unwrapped to produce the shortest arc within 360 degrees. That is, if theta2 > theta1 + 360, the arc will be from theta1 to theta2  360 and not a full circle plus some extra overlap.
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.

classmethod
circle
(center=(0.0, 0.0), radius=1.0, readonly=False)[source]¶ Return a
Path
representing a circle of a given radius and center.Parameters:  center(float, float), default: (0, 0)
The center of the circle.
 radiusfloat, default: 1
The radius of the circle.
 readonlybool
Whether the created path should have the "readonly" argument set when creating the Path instance.
Notes
The circle is approximated using 8 cubic Bezier curves, as described in
Lancaster, Don. Approximating a Circle or an Ellipse Using Four Bezier Cubic Splines.

cleaned
(self, transform=None, remove_nans=False, clip=None, quantize=<deprecated parameter>, simplify=False, curves=False, stroke_width=1.0, snap=False, sketch=None)[source]¶ Return a new Path with vertices and codes cleaned according to the parameters.
See also
Path.iter_segments
 for details of the keyword arguments.

clip_to_bbox
(self, bbox, inside=True)[source]¶ 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
numpy.uint8

property
codes
¶ The list of codes in the
Path
as a 1D numpy array. Each code is one ofSTOP
,MOVETO
,LINETO
,CURVE3
,CURVE4
orCLOSEPOLY
. For codes that correspond to more than one vertex (CURVE3
andCURVE4
), that code will be repeated so that the length ofself.vertices
andself.codes
is always the same.

contains_path
(self, path, transform=None)[source]¶ Return whether this (closed) path completely contains the given path.
If transform is not
None
, the path will be transformed before checking for containment.

contains_point
(self, point, transform=None, radius=0.0)[source]¶ Return whether the (closed) path contains the given point.
Parameters:  point(float, float)
The point (x, y) to check.
 transform
matplotlib.transforms.Transform
, optional If not
None
, point will be compared toself
transformed by transform; i.e. for a correct check, transform should transform the path into the coordinate system of point. radiusfloat, default: 0
Add an additional margin on the path in coordinates of point. The path is extended tangentially by radius/2; i.e. if you would draw the path with a linewidth of radius, all points on the line would still be considered to be contained in the area. Conversely, negative values shrink the area: Points on the imaginary line will be considered outside the area.
Returns:  bool

contains_points
(self, points, transform=None, radius=0.0)[source]¶ Return whether the (closed) path contains the given point.
Parameters:  points(N, 2) array
The points to check. Columns contain x and y values.
 transform
matplotlib.transforms.Transform
, optional If not
None
, points will be compared toself
transformed by transform; i.e. for a correct check, transform should transform the path into the coordinate system of points. radiusfloat, default: 0.
Add an additional margin on the path in coordinates of points. The path is extended tangentially by radius/2; i.e. if you would draw the path with a linewidth of radius, all points on the line would still be considered to be contained in the area. Conversely, negative values shrink the area: Points on the imaginary line will be considered outside the area.
Returns:  lengthN bool array

copy
(self)¶ Return a shallow copy of the
Path
, which will share the vertices and codes with the sourcePath
.

deepcopy
(self, memo=None)¶ Return a deepcopy of the
Path
. ThePath
will not be readonly, even if the sourcePath
is.

get_extents
(self, transform=None, **kwargs)[source]¶ Get Bbox of the path.
Parameters:  transformmatplotlib.transforms.Transform, optional
Transform to apply to path before computing extents, if any.
 **kwargs
Forwarded to
iter_bezier
.
Returns:  matplotlib.transforms.Bbox
The extents of the path Bbox([[xmin, ymin], [xmax, ymax]])

hatch
(hatchpattern, density=6)[source]¶ 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
(self, steps)[source]¶ Return a new path resampled to length N x steps.
Codes other than LINETO are not handled correctly.

intersects_bbox
(self, bbox, filled=True)[source]¶ Return whether this path intersects a given
Bbox
.If filled is True, then this also returns True if the path completely encloses the
Bbox
(i.e., the path is treated as filled).The bounding box is always considered filled.

intersects_path
(self, other, filled=True)[source]¶ Return whether if this path intersects another given path.
If filled is True, then this also returns True if one path completely encloses the other (i.e., the paths are treated as filled).

iter_bezier
(self, **kwargs)[source]¶ Iterate over each bezier curve (lines included) in a Path.
Parameters:  **kwargs
Forwarded to
iter_segments
.
Yields:  Bmatplotlib.bezier.BezierSegment
The bezier curves that make up the current path. Note in particular that freestanding points are bezier curves of order 0, and lines are bezier curves of order 1 (with two control points).
 codePath.code_type
The code describing what kind of curve is being returned. Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE4 correspond to bezier curves with 1, 2, 3, and 4 control points (respectively). Path.CLOSEPOLY is a Path.LINETO with the control points correctly chosen based on the start/end points of the current stroke.

iter_segments
(self, transform=None, remove_nans=True, clip=None, snap=False, stroke_width=1.0, simplify=None, curves=True, sketch=None)[source]¶ Iterate over all curve segments in the path.
Each iteration returns a pair
(vertices, code)
, wherevertices
is a sequence of 13 coordinate pairs, andcode
is aPath
code.Additionally, this method can provide a number of standard cleanups and conversions to the path.
Parameters:  transformNone or
Transform
If not None, the given affine transformation will be applied to the path.
 remove_nansbool, optional
Whether to remove all NaNs from the path and skip over them using MOVETO commands.
 clipNone or (float, float, float, float), optional
If not None, must be a fourtuple (x1, y1, x2, y2) defining a rectangle in which to clip the path.
 snapNone or bool, optional
If True, snap all nodes to pixels; if False, don't snap them. If None, snap if the path contains only segments parallel to the x or y axes, and no more than 1024 of them.
 stroke_widthfloat, optional
The width of the stroke being drawn (used for path snapping).
 simplifyNone or bool, optional
Whether to simplify the path by removing vertices that do not affect its appearance. If None, use the
should_simplify
attribute. See alsorcParams["path.simplify"]
(default:True
) andrcParams["path.simplify_threshold"]
(default:0.111111111111
). curvesbool, optional
If True, curve segments will be returned as curve segments. If False, all curves will be converted to line segments.
 sketchNone or sequence, optional
If not None, must be a 3tuple of the form (scale, length, randomness), representing the sketch parameters.
 transformNone or

classmethod
make_compound_path
(*args)[source]¶ Make a compound path from a list of Path objects. Blindly removes all Path.STOP control points.

classmethod
make_compound_path_from_polys
(XY)[source]¶ 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, pdf)

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

to_polygons
(self, transform=None, width=0, height=0, closed_only=True)[source]¶ Convert this path to a list of polygons or polylines. Each polygon/polyline 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.If width and height are both nonzero then the lines will be simplified so that vertices outside of (0, 0), (width, height) will be clipped.
If closed_only is
True
(default), only closed polygons, with the last point being the same as the first point, will be returned. Any unclosed polylines in the path will be explicitly closed. If closed_only isFalse
, any unclosed polygons in the path will be returned as unclosed polygons, and the closed polygons will be returned explicitly closed by setting the last point to the same as the first point.

transformed
(self, transform)[source]¶ 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
()[source]¶ Return the readonly
Path
of the unit circle.For most cases,
Path.circle()
will be what you want.

classmethod
unit_circle_righthalf
()[source]¶ Return a
Path
of the right half of a unit circle.See
Path.circle
for the reference on the approximation used.

classmethod
unit_rectangle
()[source]¶ Return a
Path
instance of the unit rectangle from (0, 0) to (1, 1).

classmethod
unit_regular_asterisk
(numVertices)[source]¶ 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)[source]¶ Return a
Path
instance for a unit regular polygon with the given numVertices such that the circumscribing circle has radius 1.0, centered at (0, 0).

classmethod
unit_regular_star
(numVertices, innerCircle=0.5)[source]¶ Return a
Path
for a unit regular star with the given numVertices and radius of 1.0, centered at (0, 0).

classmethod
wedge
(theta1, theta2, n=None)[source]¶ Return the unit circle wedge from angles theta1 to theta2 (in degrees).
theta2 is unwrapped to produce the shortest wedge within 360 degrees. That is, if theta2 > theta1 + 360, the wedge will be from theta1 to theta2  360 and not a full circle plus some extra overlap.
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.
See
Path.arc
for the reference on the approximation used.

matplotlib.path.
get_path_collection_extents
(master_transform, paths, transforms, offsets, offset_transform)[source]¶ Given a sequence of
Path
s,Transform
s objects, and offsets, as found in aPathCollection
, returns the bounding box that encapsulates all of them.Parameters: Notes
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)