Matplotlib includes a framework for arbitrary geometric transformations that is used to
determine the final position of all elements drawn on the canvas.
Transforms are composed into trees of TransformNode objects
whose actual value depends on their children. When the contents of
children change, their parents are automatically invalidated. The
next time an invalidated transform is accessed, it is recomputed to
reflect those changes. This invalidation/caching approach prevents
unnecessary recomputations of transforms, and contributes to better
interactive performance.
For example, here is a graph of the transform tree used to plot data to the figure:
The framework can be used for both affine and non-affine
transformations. However, for speed, we want to use the backend
renderers to perform affine transformations whenever possible.
Therefore, it is possible to perform just the affine or non-affine
part of a transformation on a set of data. The affine is always
assumed to occur after the non-affine. For any transform:
fulltransform==non-affine
part
+
affine
part
The backends are not expected to handle non-affine transformations
themselves.
2D affine transformations are performed using a 3x3 numpy array:
acebdf001
This class provides the read-only interface. For a mutable 2D
affine transformation, use Affine2D.
Subclasses of this class will generally only need to override a
constructor and get_matrix that generates a custom 3x3 matrix.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
frozen()
Return a frozen copy of this transform node. The frozen copy will not
be updated when its children change. Useful for storing a previously
known state of a transform where copy.deepcopy() might normally be
used.
has_inverse=True
True if this transform has a corresponding inverse transform.
input_dims=2
The number of input dimensions of this transform.
Must be overridden (with integers) in the subclass.
inverted()
Return the corresponding inverse transformation.
It holds x==self.inverted().transform(self.transform(x)).
The return value of this method should be treated as
temporary. An update to self does not cause a corresponding
update to its inverted copy.
propertyis_separable
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
output_dims=2
The number of output dimensions of this transform.
Must be overridden (with integers) in the subclass.
to_values()
Return the values of the matrix as an (a,b,c,d,e,f) tuple.
transform_affine(values)
Apply only the affine part of this transformation on the
given array of values.
transform(values) is always equivalent to
transform_affine(transform_non_affine(values)).
In non-affine transformations, this is generally a no-op. In
affine transformations, this is equivalent to
transform(values).
Parameters:
values : array
The input values as an array of length or
shape (N, ).
The base class of all affine transformations of any number of dimensions.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
get_affine()
Get the affine part of this transform.
is_affine=True
transform(values)
Apply this transformation on the given array of values.
A transform wrapper for transforming displacements between pairs of points.
This class is intended to be used to transform displacements ("position
deltas") between pairs of points (e.g., as the offset_transform
of Collections): given a transform t such that t=AffineDeltaTransform(t)+offset, AffineDeltaTransform
satisfies AffineDeltaTransform(a-b)==AffineDeltaTransform(a)-AffineDeltaTransform(b).
This is implemented by forcing the offset components of the transform
matrix to zero.
This class is experimental as of 3.3, and the API may change.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
get_matrix()
Get the matrix for the affine part of this transform.
pass_through=True
If pass_through is True, all ancestors will always be
invalidated, even if 'self' is already invalid.
Create a new Bbox from left, bottom, right and top.
The y-axis increases upwards.
Parameters:
left, bottom, right, top : float
The four extents of the bounding box.
minpos : float or None
If this is supplied, the Bbox will have a minimum positive value
set. This is useful when dealing with logarithmic scales and other
scales where negative bounds result in floating point errors.
frozen()
The base class for anything that participates in the transform tree
and needs to invalidate its parents or be invalidated. This includes
classes that are not really transforms, such as bounding boxes, since some
transforms depend on bounding boxes to compute their values.
get_points()
Get the points of the bounding box as an array of the form
[[x0,y0],[x1,y1]].
ignore(value)
Set whether the existing bounds of the box should be ignored
by subsequent calls to update_from_data_xy().
value : bool
When True, subsequent calls to update_from_data_xy will
ignore the existing bounds of the Bbox.
propertyintervalx
The pair of x coordinates that define the bounding box.
This is not guaranteed to be sorted from left to right.
propertyintervaly
The pair of y coordinates that define the bounding box.
This is not guaranteed to be sorted from bottom to top.
propertyminpos
The minimum positive value in both directions within the Bbox.
This is useful when dealing with logarithmic scales and other scales
where negative bounds result in floating point errors, and will be used
as the minimum extent instead of p0.
propertyminposx
The minimum positive value in the x-direction within the Bbox.
This is useful when dealing with logarithmic scales and other scales
where negative bounds result in floating point errors, and will be used
as the minimum x-extent instead of x0.
propertyminposy
The minimum positive value in the y-direction within the Bbox.
This is useful when dealing with logarithmic scales and other scales
where negative bounds result in floating point errors, and will be used
as the minimum y-extent instead of y0.
mutated()
Return whether the bbox has changed since init.
mutatedx()
Return whether the x-limits have changed since init.
mutatedy()
Return whether the y-limits have changed since init.
staticnull()
Create a new null Bbox from (inf, inf) to (-inf, -inf).
propertyp0
The first pair of (x, y) coordinates that define the bounding box.
This is not guaranteed to be the bottom-left corner (for that, use
min).
propertyp1
The second pair of (x, y) coordinates that define the bounding box.
This is not guaranteed to be the top-right corner (for that, use
max).
set(other)
Set this bounding box from the "frozen" bounds of another Bbox.
set_points(points)
Set the points of the bounding box directly from an array of the form
[[x0,y0],[x1,y1]]. No error checking is performed, as this
method is mainly for internal use.
Update the bounds of the Bbox to contain the vertices of the
provided path. After updating, the bounds will have positive width
and height; x0 and y0 will be the minimal values.
Parameters:
path
propertyx0
The first of the pair of x coordinates that define the bounding box.
This is not guaranteed to be less than x1 (for that, use
xmin).
propertyx1
The second of the pair of x coordinates that define the bounding box.
This is not guaranteed to be greater than x0 (for that, use
xmax).
propertyy0
The first of the pair of y coordinates that define the bounding box.
This is not guaranteed to be less than y1 (for that, use
ymin).
propertyy1
The second of the pair of y coordinates that define the bounding box.
This is not guaranteed to be greater than y0 (for that, use
ymax).
This class is immutable; Bbox is a mutable subclass.
The canonical representation is as two points, with no
restrictions on their ordering. Convenience properties are
provided to get the left, bottom, right and top edges and width
and height, but these are not stored explicitly.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
anchored(c, container)
Return a copy of the Bbox anchored to c within container.
Parameters:
c : (float, float) or {'C', 'SW', 'S', 'SE', 'E', 'NE', ...}
Either an (x, y) pair of relative coordinates (0 is left or
bottom, 1 is right or top), 'C' (center), or a cardinal direction
('SW', southwest, is bottom left, etc.).
The base class for anything that participates in the transform tree
and needs to invalidate its parents or be invalidated. This includes
classes that are not really transforms, such as bounding boxes, since some
transforms depend on bounding boxes to compute their values.
fully_contains(x, y)
Return whether x,y is in the bounding box, but not on its edge.
fully_containsx(x)
Return whether x is in the open (x0, x1) interval.
fully_containsy(y)
Return whether y is in the open (y0, y1) interval.
fully_overlaps(other)
Return whether this bounding box overlaps with the other bounding box,
not including the edges.
The first pair of (x, y) coordinates that define the bounding box.
This is not guaranteed to be the bottom-left corner (for that, use
min).
propertyp1
The second pair of (x, y) coordinates that define the bounding box.
This is not guaranteed to be the top-right corner (for that, use
max).
padded(w_pad, h_pad=None)
Construct a Bbox by padding this one on all four sides.
Parameters:
w_pad : float
Width pad
h_pad : float, optional
Height pad. Defaults to w_pad.
rotated(radians)
Return the axes-aligned bounding box that bounds the result of rotating
this Bbox by an angle of radians.
shrunk(mx, my)
Return a copy of the Bbox, shrunk by the factor mx
in the x direction and the factor my in the y direction.
The lower left corner of the box remains unchanged. Normally
mx and my will be less than 1, but this is not enforced.
Return a copy of the Bbox, shrunk so that it is as
large as it can be while having the desired aspect ratio,
box_aspect. If the box coordinates are relative (i.e.
fractions of a larger box such as a figure) then the
physical aspect ratio of that figure is specified with
fig_aspect, so that box_aspect can also be given as a
ratio of the absolute dimensions, not the relative dimensions.
propertysize
The (signed) width and height of the bounding box.
splitx(*args)
Return a list of new Bbox objects formed by splitting the original
one with vertical lines at fractional positions given by args.
splity(*args)
Return a list of new Bbox objects formed by splitting the original
one with horizontal lines at fractional positions given by args.
transformed(transform)
Construct a Bbox by statically transforming this one by transform.
translated(tx, ty)
Construct a Bbox by translating this one by tx and ty.
staticunion(bboxes)
Return a Bbox that contains all of the given bboxes.
propertywidth
The (signed) width of the bounding box.
propertyx0
The first of the pair of x coordinates that define the bounding box.
This is not guaranteed to be less than x1 (for that, use
xmin).
propertyx1
The second of the pair of x coordinates that define the bounding box.
This is not guaranteed to be greater than x0 (for that, use
xmax).
propertyxmax
The right edge of the bounding box.
propertyxmin
The left edge of the bounding box.
propertyy0
The first of the pair of y coordinates that define the bounding box.
This is not guaranteed to be less than y1 (for that, use
ymin).
propertyy1
The second of the pair of y coordinates that define the bounding box.
This is not guaranteed to be greater than y0 (for that, use
ymax).
BboxTransformFrom linearly transforms points from a given Bbox to the
unit bounding box.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
get_matrix()
Get the matrix for the affine part of this transform.
is_separable=True
True if this transform is separable in the x- and y- dimensions.
A "blended" transform uses one transform for the x-direction, and
another transform for the y-direction.
This version is an optimization for the case where both child
transforms are of type Affine2DBase.
Create a new "blended" transform using x_transform to transform the
x-axis and y_transform to transform the y-axis.
Both x_transform and y_transform must be 2D affine transforms.
You will generally not call this constructor directly but use the
blended_transform_factory function instead, which can determine
automatically which kind of blended transform to create.
get_matrix()
Get the matrix for the affine part of this transform.
is_separable=True
True if this transform is separable in the x- and y- dimensions.
A "blended" transform uses one transform for the x-direction, and
another transform for the y-direction.
This "generic" version can handle any given child transform in the
x- and y-directions.
Create a new "blended" transform using x_transform to transform the
x-axis and y_transform to transform the y-axis.
You will generally not call this constructor directly but use the
blended_transform_factory function instead, which can determine
automatically which kind of blended transform to create.
contains_branch(other)
Return whether the given transform is a sub-tree of this transform.
This routine uses transform equality to identify sub-trees, therefore
in many situations it is object id which will be used.
For the case where the given transform represents the whole
of this transform, returns True.
propertydepth
Return the number of transforms which have been chained
together to form this Transform instance.
frozen()
Return a frozen copy of this transform node. The frozen copy will not
be updated when its children change. Useful for storing a previously
known state of a transform where copy.deepcopy() might normally be
used.
get_affine()
Get the affine part of this transform.
propertyhas_inverse
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
input_dims=2
The number of input dimensions of this transform.
Must be overridden (with integers) in the subclass.
inverted()
Return the corresponding inverse transformation.
It holds x==self.inverted().transform(self.transform(x)).
The return value of this method should be treated as
temporary. An update to self does not cause a corresponding
update to its inverted copy.
propertyis_affine
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
is_separable=True
True if this transform is separable in the x- and y- dimensions.
output_dims=2
The number of output dimensions of this transform.
Must be overridden (with integers) in the subclass.
pass_through=True
If pass_through is True, all ancestors will always be
invalidated, even if 'self' is already invalid.
transform_non_affine(values)
Apply only the non-affine part of this transformation.
transform(values) is always equivalent to
transform_affine(transform_non_affine(values)).
In non-affine transformations, this is generally equivalent to
transform(values). In affine transformations, this is
always a no-op.
Parameters:
values : array
The input values as an array of length
or
shape (N, ).
A composite transform formed by applying transform a then transform b.
This version is an optimization that handles the case where both a
and b are 2D affines.
Create a new composite transform that is the result of
applying Affine2DBasea then Affine2DBaseb.
You will generally not call this constructor directly but write a+b instead, which will automatically choose the best kind of composite
transform instance to create.
propertydepth
Return the number of transforms which have been chained
together to form this Transform instance.
get_matrix()
Get the matrix for the affine part of this transform.
A composite transform formed by applying transform a then
transform b.
This "generic" version can handle any two arbitrary
transformations.
Create a new composite transform that is the result of
applying transform a then transform b.
You will generally not call this constructor directly but write a+b instead, which will automatically choose the best kind of composite
transform instance to create.
contains_branch_separately(other_transform)
Return whether the given branch is a sub-tree of this transform on
each separate dimension.
A common use for this method is to identify if a transform is a blended
transform containing an Axes' data transform. e.g.:
Return the number of transforms which have been chained
together to form this Transform instance.
frozen()
Return a frozen copy of this transform node. The frozen copy will not
be updated when its children change. Useful for storing a previously
known state of a transform where copy.deepcopy() might normally be
used.
get_affine()
Get the affine part of this transform.
propertyhas_inverse
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
inverted()
Return the corresponding inverse transformation.
It holds x==self.inverted().transform(self.transform(x)).
The return value of this method should be treated as
temporary. An update to self does not cause a corresponding
update to its inverted copy.
propertyis_affine
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
propertyis_separable
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
pass_through=True
If pass_through is True, all ancestors will always be
invalidated, even if 'self' is already invalid.
transform_affine(values)
Apply only the affine part of this transformation on the
given array of values.
transform(values) is always equivalent to
transform_affine(transform_non_affine(values)).
In non-affine transformations, this is generally a no-op. In
affine transformations, this is equivalent to
transform(values).
Parameters:
values : array
The input values as an array of length or
shape (N, ).
transform_non_affine(values)
Apply only the non-affine part of this transformation.
transform(values) is always equivalent to
transform_affine(transform_non_affine(values)).
In non-affine transformations, this is generally equivalent to
transform(values). In affine transformations, this is
always a no-op.
Parameters:
values : array
The input values as an array of length
or
shape (N, ).
transform_path_non_affine(path)
Apply the non-affine part of this transform to Pathpath,
returning a new Path.
transform_path(path) is equivalent to
transform_path_affine(transform_path_non_affine(values)).
A special class that does one thing, the identity transform, in a
fast way.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
frozen()
Return a frozen copy of this transform node. The frozen copy will not
be updated when its children change. Useful for storing a previously
known state of a transform where copy.deepcopy() might normally be
used.
get_affine()
Get the affine part of this transform.
get_matrix()
Get the matrix for the affine part of this transform.
inverted()
Return the corresponding inverse transformation.
It holds x==self.inverted().transform(self.transform(x)).
The return value of this method should be treated as
temporary. An update to self does not cause a corresponding
update to its inverted copy.
transform(values)
Apply this transformation on the given array of values.
A transformation that translates by xt and yt, after xt and yt
have been transformed by scale_trans.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
get_matrix()
Get the matrix for the affine part of this transform.
If the transform needs to do something non-standard with
matplotlib.path.Path objects, such as adding curves
where there were once line segments, it should override:
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
__add__(other)
Compose two transforms together so that self is followed by other.
A+B returns a transform C so that
C.transform(x)==B.transform(A.transform(x)).
__sub__(other)
Compose self with the inverse of other, cancelling identical terms
if any:
# In general:A-B==A+B.inverted()# (but see note regarding frozen transforms below).# If A "ends with" B (i.e. A == A' + B for some A') we can cancel# out B:(A' + B) - B == A'# Likewise, if B "starts with" A (B = A + B'), we can cancel out A:A-
contains_branch(other)
Return whether the given transform is a sub-tree of this transform.
This routine uses transform equality to identify sub-trees, therefore
in many situations it is object id which will be used.
For the case where the given transform represents the whole
of this transform, returns True.
contains_branch_separately(other_transform)
Return whether the given branch is a sub-tree of this transform on
each separate dimension.
A common use for this method is to identify if a transform is a blended
transform containing an Axes' data transform. e.g.:
Transform a set of angles anchored at specific locations.
Parameters:
angles : (N,) array-like
The angles to transform.
pts : (N, 2) array-like
The points where the angles are anchored.
radians
transform_bbox(bbox)
Transform the given bounding box.
For smarter transforms including caching (a common requirement in
Matplotlib), see TransformedBbox.
transform_non_affine(values)
Apply only the non-affine part of this transformation.
transform(values) is always equivalent to
transform_affine(transform_non_affine(values)).
In non-affine transformations, this is generally equivalent to
transform(values). In affine transformations, this is
always a no-op.
Parameters:
values : array
The input values as an array of length
or
shape (N, ).
transform_path(path)
Apply the transform to Pathpath, returning a new Path.
In some cases, this transform may insert curves into the path
that began as line segments.
transform_path_affine(path)
Apply the affine part of this transform to Pathpath, returning a
new Path.
transform_path(path) is equivalent to
transform_path_affine(transform_path_non_affine(values)).
transform_path_non_affine(path)
Apply the non-affine part of this transform to Pathpath,
returning a new Path.
transform_path(path) is equivalent to
transform_path_affine(transform_path_non_affine(values)).
transform_point(point)
Return a transformed point.
This function is only kept for backcompatibility; the more general
transform method is capable of transforming both a list of points
and a single point.
The point is given as a sequence of length input_dims.
The transformed point is returned as a sequence of length
output_dims.
The base class for anything that participates in the transform tree
and needs to invalidate its parents or be invalidated. This includes
classes that are not really transforms, such as bounding boxes, since some
transforms depend on bounding boxes to compute their values.
Parameters:
shorthand_name : str
A string representing the "name" of the transform. The name carries
no significance other than to improve the readability of
str(transform) when DEBUG=True.
frozen()
Return a frozen copy of this transform node. The frozen copy will not
be updated when its children change. Useful for storing a previously
known state of a transform where copy.deepcopy() might normally be
used.
invalidate()
Invalidate this TransformNode and triggers an invalidation of its
ancestors. Should be called any time the transform changes.
is_affine=False
pass_through=False
If pass_through is True, all ancestors will always be
invalidated, even if 'self' is already invalid.
set_children(*children)
Set the children of the transform, to let the invalidation
system know which transforms can invalidate this transform.
Should be called from the constructor of any transforms that
depend on other transforms.
A helper class that holds a single child transform and acts
equivalently to it.
This is useful if a node of the transform tree must be replaced at
run time with a transform of a different type. This class allows
that replacement to correctly trigger invalidation.
TransformWrapper instances must have the same input and output dimensions
during their entire lifetime, so the child transform may only be replaced
with another child transform of the same dimensions.
child: A Transform instance. This child may later
be replaced with set().
frozen()
Return a frozen copy of this transform node. The frozen copy will not
be updated when its children change. Useful for storing a previously
known state of a transform where copy.deepcopy() might normally be
used.
propertyhas_inverse
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
propertyinput_dims
The type of the None singleton.
propertyis_affine
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
propertyis_separable
Returns True when the argument is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.
propertyoutput_dims
The type of the None singleton.
pass_through=True
If pass_through is True, all ancestors will always be
invalidated, even if 'self' is already invalid.
set(child)
Replace the current child of this transform with another one.
The new child must have the same number of input and output
dimensions as the current child.
A Bbox that is automatically transformed by a given
transform. When either the child bounding box or transform
changes, the bounds of this bbox will update accordingly.
A TransformedPatchPath caches a non-affine transformed copy of the
Patch. This cached copy is automatically updated when the
non-affine part of the transform or the patch changes.
A TransformedPath caches a non-affine transformed copy of the
Path. This cached copy is automatically updated when the
non-affine part of the transform changes.
Return a fully-transformed copy of the child path.
get_transformed_path_and_affine()
Return a copy of the child path, with the non-affine part of
the transform already applied, along with the affine part of
the path necessary to complete the transformation.
get_transformed_points_and_affine()
Return a copy of the child path, with the non-affine part of
the transform already applied, along with the affine part of
the path necessary to complete the transformation. Unlike
get_transformed_path_and_affine(), no interpolation will
be performed.
Create a new "blended" transform using x_transform to transform
the x-axis and y_transform to transform the y-axis.
A faster version of the blended transform is returned for the case
where both child transforms are affine.
matplotlib.transforms.composite_transform_factory(a, b)
Create a new composite transform that is the result of applying
transform a then transform b.
Shortcut versions of the blended transform are provided for the
case where both child transforms are affine, or one or the other
is the identity transform.
Composite transforms may also be created using the '+' operator,
e.g.:
[Deprecated] Modify the endpoints of a range as needed to avoid singularities.
Parameters:
vmin, vmax : float
The initial endpoints.
expander : float, default: 0.001
Fractional amount by which vmin and vmax are expanded if
the original interval is too small, based on tiny.
tiny : float, default: 1e-15
Threshold for the ratio of the interval to the maximum absolute
value of its endpoints. If the interval is smaller than
this, it will be expanded. This value should be around
1e-15 or larger; otherwise the interval will be approaching
the double precision resolution limit.
increasing : bool, default: True
If True, swap vmin, vmax if vmin > vmax.
Returns:
vmin, vmax : float
Endpoints, expanded and/or swapped if necessary.
If either input is inf or NaN, or if both inputs are 0 or very
close to zero, it returns -expander, expander.
The output values as an array of length
output_dims or shape
(N, output_dims),
depending on the input.
(
A
+
B
') == B'
.
inverted
()
==
B
'^-1
Cancellation (rather than naively returning A+B.inverted()) is
important for multiple reasons:
It avoids floating-point inaccuracies when computing the inverse of
B: B-B is guaranteed to cancel out exactly (resulting in the
identity transform), whereas B+B.inverted() may differ by a
small epsilon.
B.inverted() always returns a frozen transform: if one computes
A+B+B.inverted() and later mutates B, then
B.inverted() won't be updated and the last two terms won't cancel
out anymore; on the other hand, A+B-B will always be equal to
A even if B is mutated.
array
The output values as an array of length output_dims or
shape (N, output_dims), depending on the input.
The output values as an array of length output_dims or
shape (N, output_dims), depending on the input.
: bool, default: False
Whether angles are radians or degrees.
pushoff : float
For each point in pts and angle in angles, the transformed
angle is computed by transforming a segment of length pushoff
starting at that point and making that angle relative to the
horizontal axis, and measuring the angle between the horizontal
axis and the transformed segment.