matplotlib.patches.Patch

class matplotlib.patches.Patch(edgecolor=None, facecolor=None, color=None, linewidth=None, linestyle=None, antialiased=None, hatch=None, fill=True, capstyle=None, joinstyle=None, **kwargs)[source]

Bases: matplotlib.artist.Artist

A patch is a 2D artist with a face color and an edge color.

If any of edgecolor, facecolor, linewidth, or antialiased are None, they default to their rc params setting.

The following kwarg properties are supported

Property Description
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alpha float or None
animated bool
antialiased or aa unknown
capstyle {'butt', 'round', 'projecting'}
clip_box Bbox
clip_on bool
clip_path Patch or (Path, Transform) or None
color color
contains unknown
edgecolor or ec color or None or 'auto'
facecolor or fc color or None
figure Figure
fill bool
gid str
hatch {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
in_layout bool
joinstyle {'miter', 'round', 'bevel'}
label object
linestyle or ls {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw float or None
path_effects AbstractPathEffect
picker None or bool or callable
rasterized bool or None
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
transform Transform
url str
visible bool
zorder float
__init__(self, edgecolor=None, facecolor=None, color=None, linewidth=None, linestyle=None, antialiased=None, hatch=None, fill=True, capstyle=None, joinstyle=None, **kwargs)[source]

The following kwarg properties are supported

Property Description
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alpha float or None
animated bool
antialiased or aa unknown
capstyle {'butt', 'round', 'projecting'}
clip_box Bbox
clip_on bool
clip_path Patch or (Path, Transform) or None
color color
contains unknown
edgecolor or ec color or None or 'auto'
facecolor or fc color or None
figure Figure
fill bool
gid str
hatch {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
in_layout bool
joinstyle {'miter', 'round', 'bevel'}
label object
linestyle or ls {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw float or None
path_effects AbstractPathEffect
picker None or bool or callable
rasterized bool or None
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
transform Transform
url str
visible bool
zorder float
__module__ = 'matplotlib.patches'
contains(self, mouseevent, radius=None)[source]

Test whether the mouse event occurred in the patch.

Returns:
(bool, empty dict)
contains_point(self, point, radius=None)[source]

Return whether the given point is inside the patch.

Parameters:
point(float, float)

The point (x, y) to check, in target coordinates of self.get_transform(). These are display coordinates for patches that are added to a figure or axes.

radiusfloat, optional

Add an additional margin on the patch in target coordinates of self.get_transform(). See Path.contains_point for further details.

Returns:
bool

Notes

The proper use of this method depends on the transform of the patch. Isolated patches do not have a transform. In this case, the patch creation coordinates and the point coordinates match. The following example checks that the center of a circle is within the circle

>>> center = 0, 0
>>> c = Circle(center, radius=1)
>>> c.contains_point(center)
True

The convention of checking against the transformed patch stems from the fact that this method is predominantly used to check if display coordinates (e.g. from mouse events) are within the patch. If you want to do the above check with data coordinates, you have to properly transform them first:

>>> center = 0, 0
>>> c = Circle(center, radius=1)
>>> plt.gca().add_patch(c)
>>> transformed_center = c.get_transform().transform(center)
>>> c.contains_point(transformed_center)
True
contains_points(self, points, radius=None)[source]

Return whether the given points are inside the patch.

Parameters:
points(N, 2) array

The points to check, in target coordinates of self.get_transform(). These are display coordinates for patches that are added to a figure or axes. Columns contain x and y values.

radiusfloat, optional

Add an additional margin on the patch in target coordinates of self.get_transform(). See Path.contains_point for further details.

Returns:
length-N bool array

Notes

The proper use of this method depends on the transform of the patch. See the notes on Patch.contains_point.

draw(self, renderer)[source]

Draw the Artist (and its children) using the given renderer.

This has no effect if the artist is not visible (Artist.get_visible returns False).

Parameters:
rendererRendererBase subclass.

Notes

This method is overridden in the Artist subclasses.

property fill

Return whether the patch is filled.

get_aa(self)

Alias for get_antialiased.

get_antialiased(self)[source]

Return whether antialiasing is used for drawing.

get_capstyle(self)[source]

Return the capstyle.

get_data_transform(self)[source]

Return the Transform mapping data coordinates to physical coordinates.

get_ec(self)

Alias for get_edgecolor.

get_edgecolor(self)[source]

Return the edge color.

get_extents(self)[source]

Return the Patch's axis-aligned extents as a Bbox.

get_facecolor(self)[source]

Return the face color.

get_fc(self)

Alias for get_facecolor.

get_fill(self)[source]

Return whether the patch is filled.

get_hatch(self)[source]

Return the hatching pattern.

get_joinstyle(self)[source]

Return the joinstyle.

get_linestyle(self)[source]

Return the linestyle.

get_linewidth(self)[source]

Return the line width in points.

get_ls(self)

Alias for get_linestyle.

get_lw(self)

Alias for get_linewidth.

get_patch_transform(self)[source]

Return the Transform instance mapping patch coordinates to data coordinates.

For example, one may define a patch of a circle which represents a radius of 5 by providing coordinates for a unit circle, and a transform which scales the coordinates (the patch coordinate) by 5.

get_path(self)[source]

Return the path of this patch.

get_transform(self)[source]

Return the Transform applied to the Patch.

get_verts(self)[source]

Return a copy of the vertices used in this patch.

If the patch contains Bezier curves, the curves will be interpolated by line segments. To access the curves as curves, use get_path.

get_window_extent(self, renderer=None)[source]

Get the axes bounding box in display space.

The bounding box' width and height are nonnegative.

Subclasses should override for inclusion in the bounding box "tight" calculation. Default is to return an empty bounding box at 0, 0.

Be careful when using this function, the results will not update if the artist window extent of the artist changes. The extent can change due to any changes in the transform stack, such as changing the axes limits, the figure size, or the canvas used (as is done when saving a figure). This can lead to unexpected behavior where interactive figures will look fine on the screen, but will save incorrectly.

set_aa(self, aa)

Alias for set_antialiased.

set_alpha(self, alpha)[source]

Set the alpha value used for blending - not supported on all backends.

Parameters:
alphafloat or None
set_antialiased(self, aa)[source]

Set whether to use antialiased rendering.

Parameters:
bbool or None
set_capstyle(self, s)[source]

Set the capstyle.

Parameters:
s{'butt', 'round', 'projecting'}
set_color(self, c)[source]

Set both the edgecolor and the facecolor.

Parameters:
ccolor

See also

Patch.set_facecolor, Patch.set_edgecolor
For setting the edge or face color individually.
set_ec(self, color)

Alias for set_edgecolor.

set_edgecolor(self, color)[source]

Set the patch edge color.

Parameters:
colorcolor or None or 'auto'
set_facecolor(self, color)[source]

Set the patch face color.

Parameters:
colorcolor or None
set_fc(self, color)

Alias for set_facecolor.

set_fill(self, b)[source]

Set whether to fill the patch.

Parameters:
bbool
set_hatch(self, hatch)[source]

Set the hatching pattern.

hatch can be one of:

/   - diagonal hatching
\   - back diagonal
|   - vertical
-   - horizontal
+   - crossed
x   - crossed diagonal
o   - small circle
O   - large circle
.   - dots
*   - stars

Letters can be combined, in which case all the specified hatchings are done. If same letter repeats, it increases the density of hatching of that pattern.

Hatching is supported in the PostScript, PDF, SVG and Agg backends only.

Parameters:
hatch{'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
set_joinstyle(self, s)[source]

Set the joinstyle.

Parameters:
s{'miter', 'round', 'bevel'}
set_linestyle(self, ls)[source]

Set the patch linestyle.

linestyle description
'-' or 'solid' solid line
'--' or 'dashed' dashed line
'-.' or 'dashdot' dash-dotted line
':' or 'dotted' dotted line

Alternatively a dash tuple of the following form can be provided:

(offset, onoffseq)

where onoffseq is an even length tuple of on and off ink in points.

Parameters:
ls{'-', '--', '-.', ':', '', (offset, on-off-seq), ...}

The line style.

set_linewidth(self, w)[source]

Set the patch linewidth in points.

Parameters:
wfloat or None
set_ls(self, ls)

Alias for set_linestyle.

set_lw(self, w)

Alias for set_linewidth.

update_from(self, other)[source]

Copy properties from other to self.

validCap = ('butt', 'round', 'projecting')
validJoin = ('miter', 'round', 'bevel')
zorder = 1

Examples using matplotlib.patches.Patch