matplotlib.offsetbox — Matplotlib 3.10.3 documentation (original) (raw)

Container classes for Artists.

OffsetBox

The base of all container artists defined in this module.

AnchoredOffsetbox, AnchoredText

Anchor and align an arbitrary Artist or a text relative to the parent axes or a specific anchor point.

DrawingArea

A container with fixed width and height. Children have a fixed position inside the container and may be clipped.

HPacker, VPacker

Containers for layouting their children vertically or horizontally.

PaddedBox

A container to add a padding around an Artist.

TextArea

Contains a single Text instance.

class matplotlib.offsetbox.AnchoredOffsetbox(loc, *, pad=0.4, borderpad=0.5, child=None, prop=None, frameon=True, bbox_to_anchor=None, bbox_transform=None, **kwargs)[source]#

Bases: OffsetBox

An offset box placed according to location loc.

AnchoredOffsetbox has a single child. When multiple children are needed, use an extra OffsetBox to enclose them. By default, the offset box is anchored against its parent Axes. You may explicitly specify the_bbox_to_anchor_.

Parameters:

locstr

The box location. Valid locations are 'upper left', 'upper center', 'upper right', 'center left', 'center', 'center right', 'lower left', 'lower center', 'lower right'. For backward compatibility, numeric values are accepted as well. See the parameter loc of Legend for details.

padfloat, default: 0.4

Padding around the child as fraction of the fontsize.

borderpadfloat, default: 0.5

Padding between the offsetbox frame and the bbox_to_anchor.

childOffsetBox

The box that will be anchored.

propFontProperties

This is only used as a reference for paddings. If not given,[rcParams["legend.fontsize"]](../users/explain/customizing.html?highlight=legend.fontsize#matplotlibrc-sample) (default: 'medium') is used.

frameonbool

Whether to draw a frame around the box.

bbox_to_anchorBboxBase, 2-tuple, or 4-tuple of floats

Box that is used to position the legend in conjunction with loc.

bbox_transformNone or matplotlib.transforms.Transform

The transform for the bounding box (bbox_to_anchor).

**kwargs

All other parameters are passed on to OffsetBox.

Notes

See Legend for a detailed description of the anchoring mechanism.

codes = {'center': 10, 'center left': 6, 'center right': 7, 'lower center': 8, 'lower left': 3, 'lower right': 4, 'right': 5, 'upper center': 9, 'upper left': 2, 'upper right': 1}#

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

get_bbox(renderer)[source]#

Return the bbox of the offsetbox, ignoring parent offsets.

get_bbox_to_anchor()[source]#

Return the bbox that the box is anchored to.

get_child()[source]#

Return the child.

get_children()[source]#

Return the list of children.

get_offset(bbox, renderer)[source]#

Return the offset as a tuple (x, y).

The extent parameters have to be provided to handle the case where the offset is dynamically determined by a callable (seeset_offset).

Parameters:

bboxBbox

rendererRendererBase subclass

set(*, agg_filter=, alpha=, animated=, bbox_to_anchor=, child=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_bbox_to_anchor(bbox, transform=None)[source]#

Set the bbox that the box is anchored to.

bbox can be a Bbox instance, a list of [left, bottom, width, height], or a list of [left, bottom] where the width and height will be assumed to be zero. The bbox will be transformed to display coordinate by the given transform.

set_child(child)[source]#

Set the child to be anchored.

update_frame(bbox, fontsize=None)[source]#

zorder = 5#

class matplotlib.offsetbox.AnchoredText(s, loc, *, pad=0.4, borderpad=0.5, prop=None, **kwargs)[source]#

Bases: AnchoredOffsetbox

AnchoredOffsetbox with Text.

Parameters:

sstr

Text.

locstr

Location code. See AnchoredOffsetbox.

padfloat, default: 0.4

Padding around the text as fraction of the fontsize.

borderpadfloat, default: 0.5

Spacing between the offsetbox frame and the bbox_to_anchor.

propdict, optional

Dictionary of keyword parameters to be passed to theText instance contained inside AnchoredText.

**kwargs

All other parameters are passed to AnchoredOffsetbox.

set(*, agg_filter=, alpha=, animated=, bbox_to_anchor=, child=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

class matplotlib.offsetbox.AnnotationBbox(offsetbox, xy, xybox=None, xycoords='data', boxcoords=None, *, frameon=True, pad=0.4, annotation_clip=None, box_alignment=(0.5, 0.5), bboxprops=None, arrowprops=None, fontsize=None, **kwargs)[source]#

Bases: Artist, _AnnotationBase

Container for an OffsetBox referring to a specific position xy.

Optionally an arrow pointing from the offsetbox to xy can be drawn.

This is like Annotation, but with OffsetBox instead of Text.

Parameters:

offsetboxOffsetBox

xy(float, float)

The point (x, y) to annotate. The coordinate system is determined by xycoords.

xybox(float, float), default: xy

The position (x, y) to place the text at. The coordinate system is determined by boxcoords.

xycoordssingle or two-tuple of str or Artist or Transform or callable, default: 'data'

The coordinate system that xy is given in. See the parameter_xycoords_ in Annotation for a detailed description.

boxcoordssingle or two-tuple of str or Artist or Transform or callable, default: value of xycoords

The coordinate system that xybox is given in. See the parameter_textcoords_ in Annotation for a detailed description.

frameonbool, default: True

By default, the text is surrounded by a white FancyBboxPatch(accessible as the patch attribute of the AnnotationBbox). If frameon is set to False, this patch is made invisible.

annotation_clip: bool or None, default: None

Whether to clip (i.e. not draw) the annotation when the annotation point xy is outside the Axes area.

padfloat, default: 0.4

Padding around the offsetbox.

box_alignment(float, float)

A tuple of two floats for a vertical and horizontal alignment of the offset box w.r.t. the boxcoords. The lower-left corner is (0, 0) and upper-right corner is (1, 1).

bboxpropsdict, optional

A dictionary of properties to set for the annotation bounding box, for example boxstyle and alpha. See FancyBboxPatch for details.

arrowprops: dict, optional

Arrow properties, see Annotation for description.

fontsize: float or str, optional

Translated to points and passed as mutation_scale intoFancyBboxPatch to scale attributes of the box style (e.g. pad or rounding_size). The name is chosen in analogy to Text where_fontsize_ defines the mutation scale as well. If not given,[rcParams["legend.fontsize"]](../users/explain/customizing.html?highlight=legend.fontsize#matplotlibrc-sample) (default: 'medium') is used. See Text.set_fontsize for valid values.

**kwargs

Other AnnotationBbox properties. See AnnotationBbox.set for a list.

property anncoords#

contains(mouseevent)[source]#

Test whether the artist contains the mouse event.

Parameters:

mouseeventMouseEvent

Returns:

containsbool

Whether any values are within the radius.

detailsdict

An artist-specific dictionary of details of the event context, such as which points are contained in the pick radius. See the individual Artist subclasses for details.

draw(renderer)[source]#

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

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

Parameters:

rendererRendererBase subclass.

Notes

This method is overridden in the Artist subclasses.

get_children()[source]#

Return a list of the child Artists of this Artist.

get_fontsize()[source]#

Return the fontsize in points.

get_tightbbox(renderer=None)[source]#

Like Artist.get_window_extent, but includes any clipping.

Parameters:

rendererRendererBase subclass, optional

renderer that will be used to draw the figures (i.e.fig.canvas.get_renderer())

Returns:

Bbox or None

The enclosing bounding box (in figure pixel coordinates). Returns None if clipping results in no intersection.

get_window_extent(renderer=None)[source]#

Get the artist's 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(*, agg_filter=, alpha=, animated=, annotation_clip=, clip_box=, clip_on=, clip_path=, fontsize=, gid=, in_layout=, label=, mouseover=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_figure(fig)[source]#

Set the Figure or SubFigure instance the artist belongs to.

Parameters:

figFigure or SubFigure

set_fontsize(s=None)[source]#

Set the fontsize in points.

If s is not given, reset to [rcParams["legend.fontsize"]](../users/explain/customizing.html?highlight=legend.fontsize#matplotlibrc-sample) (default: 'medium').

update_positions(renderer)[source]#

Update pixel positions for the annotated point, the text, and the arrow.

property xyann#

zorder = 3#

class matplotlib.offsetbox.AuxTransformBox(aux_transform)[source]#

Bases: OffsetBox

Offset Box with the aux_transform. Its children will be transformed with the aux_transform first then will be offsetted. The absolute coordinate of the aux_transform is meaning as it will be automatically adjust so that the left-lower corner of the bounding box of children will be set to (0, 0) before the offset transform.

It is similar to drawing area, except that the extent of the box is not predetermined but calculated from the window extent of its children. Furthermore, the extent of the children will be calculated in the transformed coordinate.

add_artist(a)[source]#

Add an Artist to the container box.

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

get_bbox(renderer)[source]#

Return the bbox of the offsetbox, ignoring parent offsets.

get_offset()[source]#

Return offset of the container.

get_transform()[source]#

Return the Transform applied to the children

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_offset(xy)[source]#

Set the offset of the container.

Parameters:

xy(float, float)

The (x, y) coordinates of the offset in display units.

set_transform(t)[source]#

set_transform is ignored.

class matplotlib.offsetbox.DraggableAnnotation(annotation, use_blit=False)[source]#

Bases: DraggableBase

save_offset()[source]#

update_offset(dx, dy)[source]#

class matplotlib.offsetbox.DraggableBase(ref_artist, use_blit=False)[source]#

Bases: object

Helper base class for a draggable artist (legend, offsetbox).

Derived classes must override the following methods:

def save_offset(self): ''' Called when the object is picked for dragging; should save the reference position of the artist. '''

def update_offset(self, dx, dy): ''' Called during the dragging; (dx, dy) is the pixel offset from the point where the mouse drag started. '''

Optionally, you may override the following method:

def finalize_offset(self): '''Called when the mouse is released.'''

In the current implementation of DraggableLegend andDraggableAnnotation, update_offset places the artists in display coordinates, and finalize_offset recalculates their position in axes coordinate and set a relevant attribute.

property canvas#

property cids#

disconnect()[source]#

Disconnect the callbacks.

finalize_offset()[source]#

on_motion(evt)[source]#

on_pick(evt)[source]#

on_release(event)[source]#

save_offset()[source]#

update_offset(dx, dy)[source]#

class matplotlib.offsetbox.DraggableOffsetBox(ref_artist, offsetbox, use_blit=False)[source]#

Bases: DraggableBase

get_loc_in_canvas()[source]#

save_offset()[source]#

update_offset(dx, dy)[source]#

class matplotlib.offsetbox.DrawingArea(width, height, xdescent=0.0, ydescent=0.0, clip=False)[source]#

Bases: OffsetBox

The DrawingArea can contain any Artist as a child. The DrawingArea has a fixed width and height. The position of children relative to the parent is fixed. The children can be clipped at the boundaries of the parent.

Parameters:

width, heightfloat

Width and height of the container box.

xdescent, ydescentfloat

Descent of the box in x- and y-direction.

clipbool

Whether to clip the children to the box.

add_artist(a)[source]#

Add an Artist to the container box.

property clip_children#

If the children of this DrawingArea should be clipped by DrawingArea bounding box.

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

get_bbox(renderer)[source]#

Return the bbox of the offsetbox, ignoring parent offsets.

get_offset()[source]#

Return offset of the container.

get_transform()[source]#

Return the Transform applied to the children.

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_offset(xy)[source]#

Set the offset of the container.

Parameters:

xy(float, float)

The (x, y) coordinates of the offset in display units.

set_transform(t)[source]#

set_transform is ignored.

class matplotlib.offsetbox.HPacker(pad=0.0, sep=0.0, width=None, height=None, align='baseline', mode='fixed', children=None)[source]#

Bases: PackerBase

HPacker packs its children horizontally, automatically adjusting their relative positions at draw time.

+-------------------------------+ | Child 1 Child 2 Child 3 | +-------------------------------+

Parameters:

padfloat, default: 0.0

The boundary padding in points.

sepfloat, default: 0.0

The spacing between items in points.

width, heightfloat, optional

Width and height of the container box in pixels, calculated if_None_.

align{'top', 'bottom', 'left', 'right', 'center', 'baseline'}, default: 'baseline'

Alignment of boxes.

mode{'fixed', 'expand', 'equal'}, default: 'fixed'

The packing mode.

childrenlist of Artist

The artists to pack.

Notes

pad and sep are in points and will be scaled with the renderer dpi, while width and height are in pixels.

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

class matplotlib.offsetbox.OffsetBox(*args, **kwargs)[source]#

Bases: Artist

The OffsetBox is a simple container artist.

The child artists are meant to be drawn at a relative position to its parent.

Being an artist itself, all parameters are passed on to Artist.

property axes#

The Axes instance the artist resides in, or None.

contains(mouseevent)[source]#

Delegate the mouse event contains-check to the children.

As a container, the OffsetBox does not respond itself to mouseevents.

Parameters:

mouseeventMouseEvent

Returns:

containsbool

Whether any values are within the radius.

detailsdict

An artist-specific dictionary of details of the event context, such as which points are contained in the pick radius. See the individual Artist subclasses for details.

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

get_bbox(renderer)[source]#

Return the bbox of the offsetbox, ignoring parent offsets.

get_children()[source]#

Return a list of the child Artists.

get_offset(bbox, renderer)[source]#

Return the offset as a tuple (x, y).

The extent parameters have to be provided to handle the case where the offset is dynamically determined by a callable (seeset_offset).

Parameters:

bboxBbox

rendererRendererBase subclass

get_visible_children()[source]#

Return a list of the visible child Artists.

get_window_extent(renderer=None)[source]#

Get the artist's 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(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_figure(fig)[source]#

Set the Figure for the OffsetBox and all its children.

Parameters:

figFigure

set_height(height)[source]#

Set the height of the box.

Parameters:

heightfloat

set_offset(xy)[source]#

Set the offset.

Parameters:

xy(float, float) or callable

The (x, y) coordinates of the offset in display units. These can either be given explicitly as a tuple (x, y), or by providing a function that converts the extent into the offset. This function must have the signature:

def offset(width, height, xdescent, ydescent, renderer) -> (float, float)

set_width(width)[source]#

Set the width of the box.

Parameters:

widthfloat

class matplotlib.offsetbox.OffsetImage(arr, *, zoom=1, cmap=None, norm=None, interpolation=None, origin=None, filternorm=True, filterrad=4.0, resample=False, dpi_cor=True, **kwargs)[source]#

Bases: OffsetBox

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

get_bbox(renderer)[source]#

Return the bbox of the offsetbox, ignoring parent offsets.

get_children()[source]#

Return a list of the child Artists.

get_data()[source]#

get_offset()[source]#

Return offset of the container.

get_zoom()[source]#

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, data=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zoom=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_data(arr)[source]#

set_zoom(zoom)[source]#

class matplotlib.offsetbox.PackerBase(pad=0.0, sep=0.0, width=None, height=None, align='baseline', mode='fixed', children=None)[source]#

Bases: OffsetBox

Parameters:

padfloat, default: 0.0

The boundary padding in points.

sepfloat, default: 0.0

The spacing between items in points.

width, heightfloat, optional

Width and height of the container box in pixels, calculated if_None_.

align{'top', 'bottom', 'left', 'right', 'center', 'baseline'}, default: 'baseline'

Alignment of boxes.

mode{'fixed', 'expand', 'equal'}, default: 'fixed'

The packing mode.

childrenlist of Artist

The artists to pack.

Notes

pad and sep are in points and will be scaled with the renderer dpi, while width and height are in pixels.

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

class matplotlib.offsetbox.PaddedBox(child, pad=0.0, *, draw_frame=False, patch_attrs=None)[source]#

Bases: OffsetBox

A container to add a padding around an Artist.

The PaddedBox contains a FancyBboxPatch that is used to visualize it when rendering.

+----------------------------+ | | | | | | | <--pad--> Artist | | ^ | | pad | | v | +----------------------------+

Attributes:

padfloat

The padding in points.

patchFancyBboxPatch

When draw_frame is True, this FancyBboxPatch is made visible and creates a border around the box.

Parameters:

childArtist

The contained Artist.

padfloat, default: 0.0

The padding in points. This will be scaled with the renderer dpi. In contrast, width and height are in pixels and thus not scaled.

draw_framebool

Whether to draw the contained FancyBboxPatch.

patch_attrsdict or None

Additional parameters passed to the contained FancyBboxPatch.

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

draw_frame(renderer)[source]#

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

update_frame(bbox, fontsize=None)[source]#

class matplotlib.offsetbox.TextArea(s, *, textprops=None, multilinebaseline=False)[source]#

Bases: OffsetBox

The TextArea is a container artist for a single Text instance.

The text is placed at (0, 0) with baseline+left alignment, by default. The width and height of the TextArea instance is the width and height of its child text.

Parameters:

sstr

The text to be displayed.

textpropsdict, default: {}

Dictionary of keyword parameters to be passed to the Textinstance in the TextArea.

multilinebaselinebool, default: False

Whether the baseline for multiline text is adjusted so that it is (approximately) center-aligned with single-line text.

draw(renderer)[source]#

Update the location of children if necessary and draw them to the given renderer.

get_bbox(renderer)[source]#

Return the bbox of the offsetbox, ignoring parent offsets.

get_multilinebaseline()[source]#

Get multilinebaseline.

get_offset()[source]#

Return offset of the container.

get_text()[source]#

Return the string representation of this area's text.

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, multilinebaseline=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, text=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are

set_multilinebaseline(t)[source]#

Set multilinebaseline.

If True, the baseline for multiline text is adjusted so that it is (approximately) center-aligned with single-line text. This is used e.g. by the legend implementation so that single-line labels are baseline-aligned, but multiline labels are "center"-aligned with them.

set_offset(xy)[source]#

Set the offset of the container.

Parameters:

xy(float, float)

The (x, y) coordinates of the offset in display units.

set_text(s)[source]#

Set the text of this area as a string.

set_transform(t)[source]#

set_transform is ignored.

class matplotlib.offsetbox.VPacker(pad=0.0, sep=0.0, width=None, height=None, align='baseline', mode='fixed', children=None)[source]#

Bases: PackerBase

VPacker packs its children vertically, automatically adjusting their relative positions at draw time.

+---------+ | Child 1 | | Child 2 | | Child 3 | +---------+

Parameters:

padfloat, default: 0.0

The boundary padding in points.

sepfloat, default: 0.0

The spacing between items in points.

width, heightfloat, optional

Width and height of the container box in pixels, calculated if_None_.

align{'top', 'bottom', 'left', 'right', 'center', 'baseline'}, default: 'baseline'

Alignment of boxes.

mode{'fixed', 'expand', 'equal'}, default: 'fixed'

The packing mode.

childrenlist of Artist

The artists to pack.

Notes

pad and sep are in points and will be scaled with the renderer dpi, while width and height are in pixels.

set(*, agg_filter=, alpha=, animated=, clip_box=, clip_on=, clip_path=, gid=, height=, in_layout=, label=, mouseover=, offset=, path_effects=, picker=, rasterized=, sketch_params=, snap=, transform=, url=, visible=, width=, zorder=)[source]#

Set multiple properties at once.

Supported properties are