text¶
matplotlib.text¶
Classes for including text in a figure.
-
class
matplotlib.text.Annotation(s, xy, xytext=None, xycoords='data', textcoords=None, arrowprops=None, annotation_clip=None, **kwargs)[source]¶ Bases:
matplotlib.text.Text,matplotlib.text._AnnotationBaseAnnotate the point
xywith texts.Additional kwargs are passed to
Text.-
anncoords¶
-
contains(event)[source]¶ Test whether the mouse event occurred in the patch.
In the case of text, a hit is true anywhere in the axis-aligned bounding-box containing the text.
Returns True or False.
-
draw(renderer)[source]¶ Draw the
Annotationobject to the given renderer.
-
get_window_extent(renderer=None)[source]¶ Return a
Bboxobject bounding the text and arrow annotation, in display units.renderer defaults to the _renderer attribute of the text object. This is not assigned until the first execution of
draw(), so you must use this kwarg if you want to callget_window_extent()prior to the firstdraw(). For getting web page regions, it is simpler to call the method after saving the figure. The dpi used defaults to self.figure.dpi; the renderer dpi is irrelevant.
-
update_positions(renderer)[source]¶ "Update the pixel positions of the annotated point and the text.
-
xyann¶
-
-
class
matplotlib.text.OffsetFrom(artist, ref_coord, unit='points')[source]¶ Bases:
objectCallable helper class for working with
AnnotationParameters: - artist :
Artist,BboxBase, orTransform The object to compute the offset from.
- ref_coord : length 2 sequence
If
artistis anArtistorBboxBase, this values is the location to of the offset origin in fractions of theartistbounding box.If
artistis a transform, the offset origin is the transform applied to this value.- unit : {'points, 'pixels'}
The screen units to use (pixels or points) for the offset input.
- artist :
-
class
matplotlib.text.Text(x=0, y=0, text='', color=None, verticalalignment='baseline', horizontalalignment='left', multialignment=None, fontproperties=None, rotation=None, linespacing=None, rotation_mode=None, usetex=None, wrap=False, **kwargs)[source]¶ Bases:
matplotlib.artist.ArtistHandle storing and drawing of text in window or data coordinates.
Create a
Textinstance at x, y with string text.Valid kwargs are
Property Description agg_filtera filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alphafloat (0.0 transparent through 1.0 opaque) animatedbool backgroundcolorany matplotlib color bboxFancyBboxPatch prop dict clip_boxa matplotlib.transforms.Bboxinstanceclip_onbool clip_path[ ( Path,Transform) |Patch| None ]colorany matplotlib color containsa callable function familyor fontfamily or fontname or name[FONTNAME | 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ] figurea Figureinstancefontpropertiesor font_propertiesa matplotlib.font_manager.FontPropertiesinstancegidan id string horizontalalignmentor ha[ 'center' | 'right' | 'left' ] labelobject linespacingfloat (multiple of font size) multialignmentor ma['left' | 'right' | 'center' ] path_effectsAbstractPathEffectpicker[None | bool | float | callable] position(x,y) rasterizedbool or None rotation[ angle in degrees | 'vertical' | 'horizontal' ] rotation_mode[ None | "default" | "anchor" ] sizeor fontsize[size in points | 'xx-small' | 'x-small' | 'small' | 'medium' | 'large' | 'x-large' | 'xx-large' ] sketch_params(scale: float, length: float, randomness: float) snapbool or None stretchor fontstretch[a numeric value in range 0-1000 | 'ultra-condensed' | 'extra-condensed' | 'condensed' | 'semi-condensed' | 'normal' | 'semi-expanded' | 'expanded' | 'extra-expanded' | 'ultra-expanded' ] styleor fontstyle[ 'normal' | 'italic' | 'oblique'] textstring or anything printable with '%s' conversion. transformTransformurla url string usetexbool or None variantor fontvariant[ 'normal' | 'small-caps' ] verticalalignmentor va[ 'center' | 'top' | 'bottom' | 'baseline' ] visiblebool weightor fontweight[a numeric value in range 0-1000 | 'ultralight' | 'light' | 'normal' | 'regular' | 'book' | 'medium' | 'roman' | 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' | 'extra bold' | 'black' ] wrapbool xfloat yfloat zorderfloat -
contains(mouseevent)[source]¶ Test whether the mouse event occurred in the patch.
In the case of text, a hit is true anywhere in the axis-aligned bounding-box containing the text.
Returns True or False.
-
get_bbox_patch()[source]¶ Return the bbox Patch object. Returns None if the FancyBboxPatch is not made.
-
get_horizontalalignment()[source]¶ Return the horizontal alignment as string. Will be one of 'left', 'center' or 'right'.
-
get_prop_tup(renderer=None)[source]¶ Return a hashable tuple of properties.
Not intended to be human readable, but useful for backends who want to cache derived information about text (e.g., layouts) and need to know if the text has changed.
-
get_usetex()[source]¶ Return whether this
Textobject uses TeX for rendering.If the user has not manually set this value, it defaults to
rcParams["text.usetex"].
-
get_verticalalignment()[source]¶ Return the vertical alignment as string. Will be one of 'top', 'center', 'bottom' or 'baseline'.
-
get_window_extent(renderer=None, dpi=None)[source]¶ Return a
Bboxobject bounding the text, in display units.In addition to being used internally, this is useful for specifying clickable regions in a png file on a web page.
renderer defaults to the _renderer attribute of the text object. This is not assigned until the first execution of
draw(), so you must use this kwarg if you want to callget_window_extent()prior to the firstdraw(). For getting web page regions, it is simpler to call the method after saving the figure.dpi defaults to self.figure.dpi; the renderer dpi is irrelevant. For the web application, if figure.dpi is not the value used when saving the figure, then the value that was used must be specified as the dpi argument.
-
static
is_math_text(s, usetex=None)[source]¶ Returns a cleaned string and a boolean flag. The flag indicates if the given string s contains any mathtext, determined by counting unescaped dollar signs. If no mathtext is present, the cleaned string has its dollar signs unescaped. If usetex is on, the flag always has the value "TeX".
-
set_backgroundcolor(color)[source]¶ Set the background color of the text by updating the bbox.
See also
set_bbox()- To change the position of the bounding box.
ACCEPTS: any matplotlib color
-
set_bbox(rectprops)[source]¶ Draw a bounding box around self. rectprops are any settable properties for a FancyBboxPatch, e.g., facecolor='red', alpha=0.5.
t.set_bbox(dict(facecolor='red', alpha=0.5))The default boxstyle is 'square'. The mutation scale of the FancyBboxPatch is set to the fontsize.
ACCEPTS: FancyBboxPatch prop dict
-
set_clip_box(clipbox)[source]¶ Set the artist's clip
Bbox.ACCEPTS: a
matplotlib.transforms.Bboxinstance
-
set_clip_on(b)[source]¶ Set whether artist uses clipping.
When False, artists will be visible outside of the axes, which can lead to unexpected results.
Parameters: - b : bool
-
set_clip_path(path, transform=None)[source]¶ Set the artist's clip path, which may be:
For efficiency, if the path happens to be an axis-aligned rectangle, this method will set the clipping box to the corresponding rectangle and set the clipping path to None.
-
set_family(fontname)[source]¶ Set the font family. May be either a single string, or a list of strings in decreasing priority. Each string may be either a real font name or a generic font class name. If the latter, the specific font names will be looked up in the
matplotlibrcfile.- ACCEPTS: [FONTNAME | 'serif' | 'sans-serif' | 'cursive' | 'fantasy' |
- 'monospace' ]
-
set_fontproperties(fp)[source]¶ Set the font properties that control the text. fp must be a
matplotlib.font_manager.FontPropertiesobject.ACCEPTS: a
matplotlib.font_manager.FontPropertiesinstance
-
set_horizontalalignment(align)[source]¶ Set the horizontal alignment to one of
ACCEPTS: [ 'center' | 'right' | 'left' ]
-
set_linespacing(spacing)[source]¶ Set the line spacing as a multiple of the font size. Default is 1.2.
ACCEPTS: float (multiple of font size)
-
set_multialignment(align)[source]¶ Set the alignment for multiple lines layout. The layout of the bounding box of all the lines is determined bu the horizontalalignment and verticalalignment properties, but the multiline text within that box can be
ACCEPTS: ['left' | 'right' | 'center' ]
-
set_rotation(s)[source]¶ Set the rotation of the text
ACCEPTS: [ angle in degrees | 'vertical' | 'horizontal' ]
-
set_rotation_mode(m)[source]¶ Set text rotation mode.
Parameters: - m :
Noneor"default"or"anchor" If
Noneor"default", the text will be first rotated, then aligned according to their horizontal and vertical alignments. If"anchor", then alignment occurs before rotation.
- m :
-
set_size(fontsize)[source]¶ Set the font size. May be either a size string, relative to the default font size, or an absolute font size in points.
- ACCEPTS: [size in points | 'xx-small' | 'x-small' | 'small' |
- 'medium' | 'large' | 'x-large' | 'xx-large' ]
-
set_stretch(stretch)[source]¶ Set the font stretch (horizontal condensation or expansion).
- ACCEPTS: [a numeric value in range 0-1000 | 'ultra-condensed' |
- 'extra-condensed' | 'condensed' | 'semi-condensed' | 'normal' | 'semi-expanded' | 'expanded' | 'extra-expanded' | 'ultra-expanded' ]
-
set_text(s)[source]¶ Set the text string s
It may contain newlines (
\n) or math in LaTeX syntax.ACCEPTS: string or anything printable with '%s' conversion.
-
set_usetex(usetex)[source]¶ Parameters: - usetex : bool or None
Whether to render using TeX,
Nonemeans to usercParams["text.usetex"].
-
set_variant(variant)[source]¶ Set the font variant, either 'normal' or 'small-caps'.
ACCEPTS: [ 'normal' | 'small-caps' ]
-
set_verticalalignment(align)[source]¶ Set the vertical alignment
ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' ]
-
set_weight(weight)[source]¶ Set the font weight.
- ACCEPTS: [a numeric value in range 0-1000 | 'ultralight' | 'light' |
- 'normal' | 'regular' | 'book' | 'medium' | 'roman' | 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' | 'extra bold' | 'black' ]
-
update_bbox_position_size(renderer)[source]¶ Update the location and the size of the bbox. This method should be used when the position and size of the bbox needs to be updated before actually drawing the bbox.
-
zorder= 3¶
-
-
class
matplotlib.text.TextWithDash(x=0, y=0, text='', color=None, verticalalignment='center', horizontalalignment='center', multialignment=None, fontproperties=None, rotation=None, linespacing=None, dashlength=0.0, dashdirection=0, dashrotation=None, dashpad=3, dashpush=0)[source]¶ Bases:
matplotlib.text.TextThis is basically a
Textwith a dash (drawn with aLine2D) before/after it. It is intended to be a drop-in replacement forText, and should behave identically to it when dashlength = 0.0.The dash always comes between the point specified by
set_position()and the text. When a dash exists, the text alignment arguments (horizontalalignment, verticalalignment) are ignored.dashlength is the length of the dash in canvas units. (default = 0.0).
dashdirection is one of 0 or 1, where 0 draws the dash after the text and 1 before. (default = 0).
dashrotation specifies the rotation of the dash, and should generally stay None. In this case
get_dashrotation()returnsget_rotation(). (i.e., the dash takes its rotation from the text's rotation). Because the text center is projected onto the dash, major deviations in the rotation cause what may be considered visually unappealing results. (default = None)dashpad is a padding length to add (or subtract) space between the text and the dash, in canvas units. (default = 3)
dashpush "pushes" the dash and text away from the point specified by
set_position()by the amount in canvas units. (default = 0)Note
The alignment of the two objects is based on the bounding box of the
Text, as obtained byget_window_extent(). This, in turn, appears to depend on the font metrics as given by the rendering backend. Hence the quality of the "centering" of the label text with respect to the dash varies depending on the backend used.Note
I'm not sure that I got the
get_window_extent()right, or whether that's sufficient for providing the object bounding box.-
draw(renderer)[source]¶ Draw the
TextWithDashobject to the given renderer.
-
get_dashpush()[source]¶ Get the extra spacing between the dash and the specified text position, in canvas units.
-
get_prop_tup(renderer=None)[source]¶ Return a hashable tuple of properties.
Not intended to be human readable, but useful for backends who want to cache derived information about text (e.g., layouts) and need to know if the text has changed.
-
get_window_extent(renderer=None)[source]¶ Return a
Bboxobject bounding the text, in display units.In addition to being used internally, this is useful for specifying clickable regions in a png file on a web page.
renderer defaults to the _renderer attribute of the text object. This is not assigned until the first execution of
draw(), so you must use this kwarg if you want to callget_window_extent()prior to the firstdraw(). For getting web page regions, it is simpler to call the method after saving the figure.
-
set_dashdirection(dd)[source]¶ Set the direction of the dash following the text. 1 is before the text and 0 is after. The default is 0, which is what you'd want for the typical case of ticks below and on the left of the figure.
ACCEPTS: int (1 is before, 0 is after)
-
set_dashpad(dp)[source]¶ Set the "pad" of the TextWithDash, which is the extra spacing between the dash and the text, in canvas units.
ACCEPTS: float (canvas units)
-
set_dashpush(dp)[source]¶ Set the "push" of the TextWithDash, which is the extra spacing between the beginning of the dash and the specified position.
ACCEPTS: float (canvas units)
-
set_figure(fig)[source]¶ Set the figure instance the artist belong to.
ACCEPTS: a
matplotlib.figure.Figureinstance
-
set_position(xy)[source]¶ Set the (x, y) position of the
TextWithDash.ACCEPTS: (x, y)
-
set_transform(t)[source]¶ Set the
matplotlib.transforms.Transforminstance used by this artist.ACCEPTS: a
matplotlib.transforms.Transforminstance
-
set_x(x)[source]¶ Set the x position of the
TextWithDash.ACCEPTS: float
-
set_y(y)[source]¶ Set the y position of the
TextWithDash.ACCEPTS: float
-

